你可以使用MVC路由来创建这个URL。您的路由表通常位于您的AppStart> RouteConfig.cs类中。您可以使用路由表为您的控制器中的操作创建URL映射。
假设我的产品是你的控制器,和鞋子,女士们,你要接受你可以这样做的变量:
routes.MapRoute("MyProducts",
"MyProducts/{category}/{subcategory}/Page",
new { controller = "MyProducts", action = "Index" });
请注意,你的路线应该是最到最具体的顺序,所以加此路线在默认路线上方。
当您导航到/我的产品/鞋/女装/页,它会映射到你的索引作用的结果在你的我的产品控制器,传递变量的类别和子类别,所以你的控制器看起来像
public class MyProducts : Controller
{
public ActionResult Index(string category, string subcategory)
{
//Do something with your variables here.
return View();
}
}
如果我的假设是错误的,你想要一个视图返回只是针对URL,你的路线将是这样的:
routes.MapRoute("MyProducts", "MyProducts/Shoes/Ladies/Page", new { controller = "MyProducts", action = "LadiesShoes" });
而且你的控制器:
public class MyProducts : Controller
{
public ActionResult LadiesShoes()
{
//Do something with your variables here.
return View();
}
}
如果您愿意,可以放心地忽略URL上的最后一个“/ page”。
如果我没有用上面的例子说明具体情况,请告诉我,我会延长我的回答。
UPDATE
你仍然可以把你的意见在一个文件夹结构,如果你想下的views文件夹 - 然后引用视图文件位置控制器 - 在下面的例子中,将您称为视图文件Index.cshtml在查看/鞋/女装/文件夹:
public class MyProducts : Controller
{
public ActionResult LadiesShoes()
{
//Do something with your variables here.
return View("~/Views/Shoes/Ladies/Index.cshtml");
}
public ActionResult MensShoes()
{
//Do something with your variables here.
return View("~/Views/Shoes/Mens/Index.cshtml");
}
}
这个回答很好,并建议您需要进行自定义路由,以获得我们认为理所当然与HTML相同的URL结构 – MyDaftQuestions
这也意味着视图我猜可以非常快速地变得非常大......当我们文件夹,它更容易组织! – MyDaftQuestions
@MyDaftQuestions,如果您想为您的视图文件复制文件夹结构,则可以覆盖视图的默认位置。我会更新我的答案 – Carl