mvc 参数字典包含空条目.....路由错误/问题

发布于 2024-11-19 14:33:09 字数 1268 浏览 3 评论 0原文

所以我有一个名为 Cars 的控制器,它有:

namespace MySite.Controllers
{
    public class CarsController : ApplicationController
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult New()
        {
            return View();
        }
        [httppost]
        public ActionResult New()
        {
            return View();
        }


        public ActionResult Details(int id)
        {
            return View();
        }
    }
}

Global asax

       routes.MapRouteLowerCase(
            "Cars", // Route name
            "Cars/{id}", // URL with parameters
            new { controller = "Cars", action = "Details", id = URLParameter.Optional}
        );

       routes.MapRouteLowerCase(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = URLParameter.Optional} // Parameter defaults
        );

所以以下是我收到的错误:

参数字典包含非参数的空条目 方法中的可为空类型

:它让我重定向到 “详细信息” 视图,并要求一个 int,即使我想转到 “新建” 视图。我不太确定发生了什么事?我基本上想让我的网址小写,同时删除促销员路由上网址上的“操作”...这有意义吗? 任何帮助表示赞赏!谢谢!!!

So I have this controller called Cars, and it has:

namespace MySite.Controllers
{
    public class CarsController : ApplicationController
    {

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult New()
        {
            return View();
        }
        [httppost]
        public ActionResult New()
        {
            return View();
        }


        public ActionResult Details(int id)
        {
            return View();
        }
    }
}

Global asax

       routes.MapRouteLowerCase(
            "Cars", // Route name
            "Cars/{id}", // URL with parameters
            new { controller = "Cars", action = "Details", id = URLParameter.Optional}
        );

       routes.MapRouteLowerCase(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = URLParameter.Optional} // Parameter defaults
        );

So following is the error I'm getting:

the parameters dictionary contains a null entry for parameter of non
nullable type for method in

NOTE: It's getting me redirected to the "Details" View and is asking for an int even thought I wanted to go to the "New" view. I'm not really sure what's happening? I basically wanted to have my urls lowercased while removing the "action" on the url on the Promoter routing...Does that make sense?
Anyhelp is appreciated! Thanks!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

扭转时空 2024-11-26 14:33:09

下面的所有代码都适用于第一个路由定义。
第二条路线定义应保持原样。

您的路线无效

看一下第一个路线定义:

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details", id = URLParameter.Optional}
);

当您访问Cars/New时,第一个路线会被命中,因为所有参数很容易应用为:

  • controller = "Cars"
  • action = "Details"
  • id = "New"

如果您想要第一条路线定义仅覆盖某些 ID,您应该对其施加约束或更改你的路由。数字 ID 的约束应如下所示:

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details" },
    new { id = @"\d+" }
);

当您访问 Cars/New 时,不会命中第一条路线,因为 New 不满足 ID 约束,因此路线处理将继续下一条路线(这会很好地解决它 - 正如它应该的那样)。

请注意,id 不再是可选的。如果对其施加约束,那就不可能了。如果您有一个 Details 控制器操作,它很可能应该显示一些特定的详细信息。所以它实际上需要一些ID。

All code below applies to the first route definition.
Second route definition should stay as it is.

Your routing is invalid

Take a look at the first route definition:

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details", id = URLParameter.Optional}
);

And when you access Cars/New this first route gets hit because all parameters are easily applied as:

  • controller = "Cars"
  • action = "Details"
  • id = "New"

If you'd like the first route definition to only cover certain IDs you should put a constraint onto it or change your routing. Constraint for numeric IDs should look like this:

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details" },
    new { id = @"\d+" }
);

When you'd access Cars/New first route wouldn't be hit because New doesn't satisfy ID constraint so route processing would continue with the next route (which would resolve it just fine - as it should).

Mind the fact, that id isn't optional anymore. In case of putting a constraint onto it it can't be. If you have a Details controller action it should most probably display some certain details. So it actually needs some ID.

败给现实 2024-11-26 14:33:09

如果您的路线设置如下,

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details", id = URLParameter.Optional}
);

则意味着任何以“/Cars/...”开头的 URL 都会将您重定向到“详细信息”操作,其中包括“/Cars/New”,并且它将尝试将“New”字符串映射到id 参数就是您收到错误的原因。

If your route is setted like this

routes.MapRouteLowerCase(
    "Cars", // Route name
    "Cars/{id}", // URL with parameters
    new { controller = "Cars", action = "Details", id = URLParameter.Optional}
);

that means that any URL that starts with "/Cars/..." will redirect you to Details action, that includes "/Cars/New" and it will try to map the "New" string to the id parameter that is why you get the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文