ASP.NET MVC 路由 - 尝试在 URL 中包含名称

发布于 2024-07-29 06:27:45 字数 1034 浏览 3 评论 0原文

我目前有以下路线:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗地喜欢 2024-08-05 06:27:45

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

// 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) {...}

I propose the solution was your route variable names did not match your action parameter names.

// 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();

This would work:

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

Where this wouldn't:

// Controller.cs
public ActionResult Index(int otherID) {...}
寄意 2024-08-05 06:27:45

这与错误消息所说的完全一样。 您有一个名为“ID”的参数,它没有默认值,但您的方法需要一个不可为 null 的 int。 因为没有默认值,所以它尝试传入“null”,但是......它不能,因为您的 int 参数不可为空。

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

要解决这个问题:

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

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:

 MvcRoute.MappUrl("{title}/{ID}")
        .WithDefaults(new { controller = "special", action = "Index", ID = 0 })
        .AddWithName("view", routes)
        .RouteHandler = new MvcRouteHandler();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文