我目前有以下路线:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.gif/{*pathInfo}");
MvcRoute.MappUrl("{controller}/{action}/{ID}")
.WithDefaults(new { controller = "home", action = "index", ID = 0 })
.WithConstraints(new { controller = "..." })
.AddWithName("default", routes)
.RouteHandler = new MvcRouteHandler();
MvcRoute.MappUrl("{title}/{ID}")
.WithDefaults(new { controller = "special", action = "Index" })
.AddWithName("view", routes)
.RouteHandler = new MvcRouteHandler();
SpecialController 有一个方法:public ActionResult Index(int ID)
每当我将浏览器指向 http://hostname/test/5
时,我都会收到以下错误:
The parameters dictionary contains a null entry for parameter 'ID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'SpecialController'. To make a parameter optional its type should be either a reference type or a Nullable type.
Parameter name: parameters
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
这是为什么呢?我使用了 mvccontrib 路由调试器,看起来路由可以按预期访问。
最佳答案
我认为您应该将自定义路由放在默认路由之前。
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
关于asp.net-mvc - ASP.NET MVC 路由 - 尝试在 URL 中有一个名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1182768/