当 MVC 路由触发时,在控制器中获取空参数值

发布于 2024-10-12 00:06:33 字数 1234 浏览 8 评论 0原文

首先,我是 MVC 新手,所以如果这个问题很基础,请原谅。

我使用自定义路由创建以下 URL (http://mysite/subscriber/12345),其中12345 是订户号码。我希望它在 Subscriber 控制器中运行 ShowAll 操作。我的路线正在触发并使用 Phil 的路线调试器,当我传入上面的 url 时,路由调试器显示 ID 为 12345。 我的控制器接受 int 作为 subscriberID。当它火起来的时候, 控制器抛出错误

参数字典包含不可为空类型“System.Int32”的参数“id”的空条目。

为什么路由调试器显示一个值而控制器看不到它?

这是我的路由(第一个是罪魁祸首)

 routes.MapRoute(
              "SubscriberAll",
              "subscriber/{id}",
              new { controller = "Subscriber", action = "ShowAll", id=0 },
              new { id = @"\d+" } //confirm numeric
            );

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

知道为什么我在 ShowAll 操作中得到 null 吗?这是操作方法签名:

 public ActionResult ShowAll(int id)

First off, I'm new to MVC, so please excuse the question if it's basic.

I'm using a custom route to create the following URL (http://mysite/subscriber/12345) where 12345 is the subscriber number. I want it to run the ShowAll action in the Subscriber controller. My route is firing and using Phil's route debugger, when I pass in the above url, the route debugger shows ID as 12345.
My controller is accepting an int as subscriberID. When it fires,
the controller throws the error

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32".

Why does the route debugger show a value and the controller doesn't see it?

Here's my route (first one is the culprit)

 routes.MapRoute(
              "SubscriberAll",
              "subscriber/{id}",
              new { controller = "Subscriber", action = "ShowAll", id=0 },
              new { id = @"\d+" } //confirm numeric
            );

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

Any idea why I'm getting a null in the ShowAll action? Here is the action method signature:

 public ActionResult ShowAll(int id)

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

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

发布评论

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

评论(3

野却迷人 2024-10-19 00:06:33

发现控制器方法签名需要接受一个字符串,因为 MVC 不知道传递参数是什么类型,因此无法将其强制转换为 int,但它可以通过约束强制执行。

所以,我最终的路线是这样的:

路线.MapRoute(
“订阅者全部”,
“订阅者/{id}”,
新的{controller =“订阅者”,操作=“ShowAll”},
new {id = @"\d+" } //确认数字
);

我最终得到的控制器方法签名是这样的

 public ActionResult ShowAll(string id)

Found that the controller method signature needs to accept a string as MVC doesn't know what type the passing parameter is and therefore can't cast it to int, but it can enforce it through the constraint.

So, the route I ended up with is this:

routes.MapRoute(
"SubscriberAll",
"subscriber/{id}",
new {controller = "Subscriber", action = "ShowAll" },
new {id = @"\d+" } //confirm numeric
);

and the controller method signature I ended up with is this

 public ActionResult ShowAll(string id)
一世旳自豪 2024-10-19 00:06:33

尝试从默认值列表中删除 id,即只有

new { controller = "Subscriber", action = "ShowAll" }

Try removing id from the list of defaults ie just have

new { controller = "Subscriber", action = "ShowAll" }
月下伊人醉 2024-10-19 00:06:33

不要在 MapRoute 中写入“id = 0”,而是写入“id = UrlParameter.Optional”,

这肯定会与您的操作结果配合使用

public ActionResult ShowAll(int id)

routes.MapRoute(
                  "SubscriberAll",
                  "subscriber/{id}",
                  new { controller = "Subscriber", action = "ShowAll", id = UrlParameter.Optional },
                  new { id = @"\d+" } //confirm numeric
                );

Instead of writing "id = 0" in your MapRoute write "id = UrlParameter.Optional"

this would definitely work with your action result

public ActionResult ShowAll(int id)

routes.MapRoute(
                  "SubscriberAll",
                  "subscriber/{id}",
                  new { controller = "Subscriber", action = "ShowAll", id = UrlParameter.Optional },
                  new { id = @"\d+" } //confirm numeric
                );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文