使用 MVC 路由缩短 URL 参数

发布于 2024-08-01 18:10:29 字数 717 浏览 3 评论 0原文

我有一个类似这样的网址:

example.com/profile/publicview?profileKey=5

我想通过路由将其缩短为此

example.com/profile/5

我该怎么做?

这是我的尝试:

routes.MapRoute(
    "Profile", "Profile/{profileKey}", 
    new { controller = "Profile", action = "PublicView", profileKey ="" }
);

但是他产生了这个错误:

参数字典包含一个 参数“profileKey”的空条目 不可为空类型“System.Int32” 对于方法 'System.Web.Mvc.ActionResult PublicView(Int32)' 中 '网站.Controllers.ProfileController

操作方法

public ActionResult PublicView(int profileKey)
  {
        //stuff
  }

I have a url along the lines of this:

example.com/profile/publicview?profileKey=5

and I want to shorten it to this with routing

example.com/profile/5

How do I do this?

This is my attempt:

routes.MapRoute(
    "Profile", "Profile/{profileKey}", 
    new { controller = "Profile", action = "PublicView", profileKey ="" }
);

But his produces this error:

The parameters dictionary contains a
null entry for parameter 'profileKey'
of non-nullable type 'System.Int32'
for method
'System.Web.Mvc.ActionResult
PublicView(Int32)' in
'Website.Controllers.ProfileController

Action method

public ActionResult PublicView(int profileKey)
  {
        //stuff
  }

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

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

发布评论

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

评论(4

风和你 2024-08-08 18:10:29

在控制器上更改 PublicView(int? profileKey)

注释的答案

是的,但由于路由中的默认值为 null,因此您需要处理它。 否则,请更改路由中的默认值。

也许

这可能没有帮助,但值得一试。 这是我的网站的代码:

        routes.MapRoute(
            "freebies",                                              // Route name
            "freebies/{page}",                           // URL with parameters
            new { controller = "Home", action = "Freebies", page = 1 }  // Parameter defaults
        );

和链接:

http://localhost/freebies/2

这工作正常。

On the controller change the PublicView(int? profileKey)

ANSWER TO COMMENT

Yes, but since your default value in the route is null, you need to handle it. Otherwise, change the default in your route.

MAYBE

This probably won't help, but it is worth a try. Here is the code I have my site:

        routes.MapRoute(
            "freebies",                                              // Route name
            "freebies/{page}",                           // URL with parameters
            new { controller = "Home", action = "Freebies", page = 1 }  // Parameter defaults
        );

And the link:

http://localhost/freebies/2

And this works fine.

掐死时间 2024-08-08 18:10:29

当且仅当您始终在网址中包含整数值(如 example.com/profile/5 中所示)时,您的路线和操作方法就可以按原样使用。 (我假设此路由是在默认路由之上定义的)

您定义的是一个路由,如果 url 中未提供 profileKey,则将其默认为空字符串,并且在您尝试将此值绑定到的操作方法中一个不可为 null 的整数,因此如果您尝试 example.com/profileexample.com/profile/not-an-int 这将引发您报告的异常。 您的路线将为 profileKey 分配一个空字符串,但您的操作方法无法将其转换为整数。 正如 Martin 指出的,解决这个问题的一种方法是使用而是一个可为 null 的 int。 另一种解决方案是将路由中的该值默认为整数。

如果您仍然收到错误消息,请确保您请求的实际 URL 正确并且包含 profileKey。 我知道有时在使用默认的 html 和/或 url 帮助程序时,我会在传递 routeValues 时犯一个错误,并最终渲染链接或发布到与我预期不同的 url...

希望有帮助

Your route and action method are fine as-is, if and only if, you are always including an integer value in your url as in example.com/profile/5. (I assume this route is defined above the Default route)

What you've defined is a route that defaults profileKey to an empty string if it is not provided in the url, and within your action method you've attempted to bind this value to a non-nullable integer, so if you are attempting example.com/profile or example.com/profile/not-an-int this will throw the exception you are reporting. Your route will assign an empty string to profileKey, but your action method fails to cast this as an integer. As Martin pointed out, one way around this is to use a nullable int instead. An alternative solution would be to default this value in your route to an integer.

If you are still getting the error, make sure the actual URL you are requesting is correct and has the profileKey included. I know sometimes when using the default html and/or url helpers I make a mistake passing in the routeValues and end up rendering a link or posting to a url other than what I was expecting...

Hope that helps

落日海湾 2024-08-08 18:10:29

这是你唯一的路线吗? 如果不是,则可能是另一个路由(之前声明的)首先匹配,并且没有正确获取 profileKey 参数。

Is that your only route? If not, maybe another route (declared earlier) is matching first, and isn't picking up the profileKey parameter properly.

银河中√捞星星 2024-08-08 18:10:29

试试这个(未经测试的)代码

routes.MapRoute(
    "Profile", "Profile/{profileKey}", 
    new { controller = "Profile", action = "PublicView" }
);

Try this (untested) code

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