[英]MVC4 MapRoute for CMS
Am building a CMS using MVC4. 我正在使用MVC4构建CMS。 My experience with MVC is limited and trying to create a MapRoute that can handle the page structured created by the CMS.
我在MVC方面的经验有限,并且试图创建一个MapRoute来处理CMS创建的结构化页面。 URLs for the pages would be along the lines of website.com/About
页面的网址应与website.com/About有关
To handle this I have come up with the following 为了解决这个问题,我提出了以下建议
routes.MapRoute(
name: "Default",
url: "{p}",
defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
);
This works fine for root level pages but if I want sub pages like website.com/About/OurTeam 这对于根级页面来说效果很好,但是如果我想要像website.com/About/OurTeam这样的子页面
I get a 404. Ideally what I would like is just be able pass either the whole url after the .com bit as a string and sort it out in the controller or to split the levels up as an array of parameters and pass that through as 'p'. 我得到404。理想情况下,我只希望能够将.com位之后的整个url作为字符串传递并在控制器中进行排序,或者将级别拆分为参数数组,然后将其作为'p'。
Hope that makes sense. 希望有道理。 Any ideas?
有任何想法吗?
You can use: 您可以使用:
routes.MapRoute(
name: "Default",
url: "{*p}",
defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
);
The asterisk indicates that it's a catch-all route. 星号表示这是一条通吃的路线。 Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.
请记住,这些路线非常贪婪,请确保该路线不位于任何特定路线之下。
You could also add a route constraint to this route which can determine whether the page exists in the database or something. 您还可以向该路由添加路由约束,以确定该页面是否存在于数据库中或其他内容。 See this post for more info.
有关更多信息,请参见此帖子 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.