ASP.NET MVC 路由 - 尝试在 URL 中包含名称
我目前有以下路线:
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
时,我收到以下错误:
参数字典包含“SpecialController”中方法“System.Web.Mvc.ActionResult Index(Int32)”的不可空类型“System.Int32”的参数“ID”的空条目。 要使参数可选,其类型应该是引用类型或 Nullable 类型。
参数名称:参数
描述:执行当前 Web 请求期间发生未处理的异常。 请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
这是为什么? 我使用了 mvccontrib 路由调试器,看起来路由可以按预期访问。
I have currently the following routes:
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();
The SpecialController has a method: public ActionResult Index(int ID)
Whenever I point my browser to http://hostname/test/5
, I get the following error:
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.
Why is that? I used the mvccontrib route debugger, and it seems that the route is accessible as expected.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您应该将自定义路线放在默认路线之前。
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
I think you should put your custom route before the default one.
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
我建议的解决方案是您的路由变量名称与您的操作参数名称不匹配。
这会起作用:
如果这不起作用:
I propose the solution was your route variable names did not match your action parameter names.
This would work:
Where this wouldn't:
这与错误消息所说的完全一样。 您有一个名为“ID”的参数,它没有默认值,但您的方法需要一个不可为 null 的 int。 因为没有默认值,所以它尝试传入“null”,但是......它不能,因为您的 int 参数不可为空。
路由调试器可能不会检查类型是否可为空。
要解决这个问题:
It's exactly as the error message says. You've got a parameter called "ID" which has no default value, but your method is expecting a non-nullable int. Because there is no default value, it's trying to pass in a "null", but... it can't, because your int parameter is non-nullable.
The route debugger probably doesn't check types for nullable.
To fix it: