asp.net-mvc - ASP.NET MVC 路由 - 尝试在 URL 中有一个名称

标签 asp.net-mvc asp.net-mvc-routing

我目前有以下路线:

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/

相关文章:

c# - MVC4 EF代码在生产中首先数据库初始化不完整

asp.net-mvc - 如果不点击 Microsoft 登录页面,则无法访问 Azure 上的 Web 应用程序

c# - 开发 Multi-Tenancy asp.net MVC 应用程序时要记住什么?

c# - 如何为泛型类编写 C# 扩展方法

asp.net-mvc - 在设置 TempData ["myvalue"] = "foo"之前调用 MVC 3 Session.Abandon() 导致下一个 Controller 将 TempData 设置为 null

asp.net - 没有详细信息操作的 ASP.MVC 路由

ASP.NET Mvc 5 返回带有 seo 友好 url 的 View

c# - MVC3路由问题

asp.net-mvc - 如何在 MVC 选择路由之前添加路由参数

c# - ASP.NET MVC 显示用户全名