如何从控制器内重定向到特定 URL?
我试图在用户提交表单后将他们重定向到特定地址。 我在内容控制器中使用以下代码:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
代码实际上应该是相同的,但要注意的是在 Global.asax 文件中,我在其中设置路由表,我需要以下代码段才能产生我想要的结果。请注意该代码中的第三个 url 参数 {parameter}
,我只需要在代码中使用变量“parameter”而不是“Id”:
为变量使用与 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}
with that code, I only need to use the variable "parameter" instead of "Id" in my code:
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
看看
return RedirectToAction("Business" , "Content" , new { Id =business.BusinessID });
Check this out
return RedirectToAction("Business" , "Content" , new { Id = business.BusinessID });
对我来说,您的代码行产生了您期望的链接,您的路由设置好吗?
For me, your line of code produces the link that you are expecting,is your routing setup OK?