ASPNET MVC路由问题

Cri*_*ris 2 asp.net-mvc asp.net-mvc-routing

我需要添加一个http:// mysite/categoryname路由,所以我添加了

routes.MapRoute(
     "Categories",
     "{CategoryName}",
     new { controller = "News", action = "Category", CategoryName = "" },
     new string[] { "MyProj.Controllers" }
        );
Run Code Online (Sandbox Code Playgroud)

问题是,如果我之前添加它

routes.MapRoute(
     "Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new { controller = "News", action = "Index", id = UrlParameter.Optional }, 
     new string[] { "MyProj.Controllers" }
        );
Run Code Online (Sandbox Code Playgroud)

主页出错,因为它进入类别路线; 如果我在最后位置添加类别路线,则永远不会输入,http:// mysite/category_name给我404.

我究竟做错了什么?

Kei*_*ith 5

你有几个选择:

更改您的新闻类别路线以包含硬路径:

routes.MapRoute(
     "Categories",
     "category/{CategoryName}",
     new { controller = "News", action = "Category", CategoryName = "" },
     new string[] { "MyProj.Controllers" }
        );
Run Code Online (Sandbox Code Playgroud)

更改默认路由以包含硬路径:

routes.MapRoute(
     "Default", // Route name
     "site/{controller}/{action}/{id}", // URL with parameters
     new { controller = "News", action = "Index", id = UrlParameter.Optional }, 
     new string[] { "MyProj.Controllers" }
     );
Run Code Online (Sandbox Code Playgroud)

滚动您自己的自定义路由类

有关不相关的示例,请参见http://hanssens.org/post/ASPNET-MVC-Subdomain-Routing.aspx.