你如何重定向到ASP.NET MVC调用页面? [英] How do you redirect to the calling page in ASP.NET MVC?
问题描述
让我们说我有一个控制器动作,删除一个项目出了用户的购物篮中。该控制器操作是通过执行一个POST到URL触发〜/删除/ {显示}。如果我有我的应用程序几个页面,将张贴到这个网址,我怎么构建控制器动作重定向回到那个贴吧的页面?
Let's say I have a controller action that deletes an item out of a user's shopping basket. This controller action is triggered by performing a POST to the url ~/delete/{id}. If I have several pages on my application that will post to this url, how do I construct the controller action to redirect back to the page that posted to it?
推荐答案
您应该提供从发帖页面RedirectToUrl参数。
You should provide a RedirectToUrl parameter from the posting page.
依托引荐头是不是一个好的做法。
Relying on referrer headers is not a good practice.
相反,做这样的事情:
public ActionResult Delete(int id, string RedirectToUrl)
{
// check if RedirectToUrl is null or empty and redirect accordingly
}
在发布视图或局部视图,你可以提供的参数在几个方面:
On the posting view or partial view you can provide the parameter in several ways:
<%= Html.Hidden("RedirecToUrl","/my/lovely/url") %>
或
<form action="/item/delete/22?RedirectToUrl=/my/lovely/url">
我preFER第一个选项。
I'd prefer the first option.
这篇关于你如何重定向到ASP.NET MVC调用页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!