【问题标题】:ASP.NET MVC routing - trying to have a name in the URLASP.NET MVC 路由 - 试图在 URL 中有一个名称
【发布时间】:2009-07-25 18:59:03
【问题描述】:

我目前有以下路线:

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路由调试器,看来路由可以按预期访问了。

【问题讨论】:

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


    【解决方案1】:

    我认为您应该将自定义路由放在默认路由之前。

    http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

    【讨论】:

    • +1 我想你可能就在这里。另请查看 Phil Haack 的文章重新路由调试:haacked.com/archive/2008/03/13/url-routing-debugger.aspx
    • 不幸的是,这也不是解决方案。但你是对的,默认值应该在最后被视为最后的手段。
    • 恐怕这可能是 mvccontrib 中的一个缺陷。我通过将路由直接映射到 RouteCollection(不使用 mvccontrib)来尝试您的方案,它按预期工作。
    • routes.MapRoute("special", "{title}/{ID}", new { controller = "special", action = "Index" } ); routes.MapRoute( "Default", // 路由名称 "{controller}/{action}/{id}", // 带参数的 URL new { controller = "Home", action = "Index", id = "" } // 参数默认值 );
    • 还是不行...控制器有index方法如下: public SpecialControllere() { // init stuff } public ActionResult Index(int ID) { // do stuff return View(form) ; }
    【解决方案2】:

    我建议解决方案是您的路由变量名称与您的操作参数名称不匹配。

    // Global.asax.cs
    MvcRoute.MappUrl("{controller}/{action}/{ID}")
            .WithDefaults(new { controller = "home", action = "index", ID = 0 })
            .WithConstraints(new { controller = "..." })
            .AddWithName("default", routes)
            .RouteHandler = new MvcRouteHandler();
    

    这可行:

    // Controller.cs
    public ActionResult Index(int ID){...}
    

    这不会:

    // Controller.cs
    public ActionResult Index(int otherID) {...}
    

    【讨论】:

    • 我不熟悉你上面的路由代码模式。 .WithConstraints(new { controller = "..." }) 和 .AddWithName("default", routes) 的含义是什么?这里的控制器名称是什么?
    【解决方案3】:

    正如错误消息所说。您有一个名为“ID”的参数,它没有默认值,但您的方法需要一个不可为空的 int。因为没有默认值,它试图传入一个“null”,但是......它不能,因为你的 int 参数是不可为空的。

    路由调试器可能不会检查类型是否可以为空。

    修复它:

     MvcRoute.MappUrl("{title}/{ID}")
            .WithDefaults(new { controller = "special", action = "Index", ID = 0 })
            .AddWithName("view", routes)
            .RouteHandler = new MvcRouteHandler();
    

    【讨论】:

    • 我不认为是这种情况,即使在我添加了默认 ID(我之前也尝试过)之后它仍然会抛出相同的异常。
    • 那不是我的代码。这是 mvccontrib 的一部分,我没有可用的源代码
    • 玩了一个多小时后,现在的视图配置如下。但是,问题仍然存在...... MvcRoute.MappUrl("{controller}/{ID}/{questionTitle}") .WithDefaults(new { controller = "special", action = "Index", ID = 0 }) 。 AddWithName("view", routes) .RouteHandler = new MvcRouteHandler(); -- 省略默认路由
    • 我不熟悉你上面的路由代码模式。 .AddWithName("view", routes) 是什么意思?
    猜你喜欢
    • 2011-12-19
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2012-12-13
    相关资源
    最近更新 更多