如何从控制器内重定向到特定 URL?

发布于 2024-12-18 10:16:12 字数 414 浏览 5 评论 0原文

我试图在用户提交表单后将他们重定向到特定地址。 我在内容控制器中使用以下代码:

return RedirectToAction("Business", new RouteValueDictionary(new { controller = "Content", action = "Business", Id = business.BusinessID }));

它创建一个如下所示的 URL: www.x.com/Content/Business?Id=13

我真正想要的是这样的: www.x.com/Content/Business/13

我真的不希望查询字符串用 ? 分隔但要与其余 URL 保持一致。

有办法这样做吗?

I am trying to redirect user to a specific address, after they submit a form.
What I use is the code below in my Content controller:

return RedirectToAction("Business", new RouteValueDictionary(new { controller = "Content", action = "Business", Id = business.BusinessID }));

It creates a URL like this :
www.x.com/Content/Business?Id=13

What I really want is something like this:
www.x.com/Content/Business/13

I really don't want the query string to be separated with ? but to be consistent with the rest of the URLs.

Is there anyway to do so?

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

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

发布评论

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

评论(3

秋日私语 2024-12-25 10:16:12

代码实际上应该是相同的,但要注意的是在 Global.asax 文件中,我在其中设置路由表,我需要以下代码段才能产生我想要的结果。请注意该代码中的第三个 url 参数 {parameter}

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

,我只需要在代码中使用变量“parameter”而不是“Id”:

return RedirectToAction("Business", new RouteValueDictionary(new { controller = "Content", action = "Business", parameter= business.BusinessID }));

为变量使用与 MapRout() 中定义的名称相同的名称非常重要功能。

这样做,我得到了我想要的结果:

www.x.com/Content/Business/13

The code should actually be the same thing, but the thing to note is in the Global.asax file, where I setup my routing table, I need to have the following piece of code so that my desired result be produced. Notice the third url parameters {parameter}

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

with that code, I only need to use the variable "parameter" instead of "Id" in my code:

return RedirectToAction("Business", new RouteValueDictionary(new { controller = "Content", action = "Business", parameter= business.BusinessID }));

It is very important to use the same name for your variable as you defined in MapRout() function.

Doing this, I have my desired result :

www.x.com/Content/Business/13

梦年海沫深 2024-12-25 10:16:12

看看

return RedirectToAction("Business" , "Content" , new { Id =business.BusinessID });

Check this out

return RedirectToAction("Business" , "Content" , new { Id = business.BusinessID });

我要还你自由 2024-12-25 10:16:12

对我来说,您的代码行产生了您期望的链接,您的路由设置好吗?

For me, your line of code produces the link that you are expecting,is your routing setup OK?

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