简体   繁体   English

如何重定向到ASP.NET MVC中的调用页面?

[英]How do you redirect to the calling page in ASP.NET MVC?

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}. 通过对url~ / delete / {id}执行POST来触发此控制器操作。 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? 如果我的应用程序上有多个页面将发布到此URL,我该如何构建控制器操作以重定向回发布到它的页面?

That's what I do: 我就是做这个的:

    public ActionResult ResendActivationEmail()
    {
        // Do other things here
        return new RedirectResult(Request.UrlReferrer.AbsoluteUri);
    }

You should provide a RedirectToUrl parameter from the posting page. 您应该从发布页面提供RedirectToUrl参数。

Relying on referrer headers is not a good practice. 依赖引用标头不是一个好习惯。

Instead, do something like this: 相反,做这样的事情:

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") %>

or 要么

<form action="/item/delete/22?RedirectToUrl=/my/lovely/url">

I'd prefer the first option. 我更喜欢第一种选择。

The first thing that I would do is use Ajax.ActionLink, then if the user has Javascript turned on, you'd never actually leave the page. 我要做的第一件事是使用Ajax.ActionLink,然后如果用户打开了Javascript,你就永远不会离开页面。 This is the best solution. 这是最好的解决方案。 If you don't want a link, you could also have an Ajax form. 如果您不想要链接,也可以使用Ajax表单。 Either of these could use the DELETE or POST method. 其中任何一个都可以使用DELETE或POST方法。

In order to handle the case where Javascript is turned off, when you detect in the controller that the POST was not done with Ajax (Request.IsAjaxRequest is false), you could look at the Request.UrlReferer property to get the Url of the referring page. 为了处理关闭Javascript的情况,当您在控制器中检测到POST没有使用Ajax(Request.IsAjaxRequest为false)时,您可以查看Request.UrlReferer属性以获取引用的Url页。 If this is not null, you can use a RedirectResult to go back to this page. 如果这不为null,则可以使用RedirectResult返回此页面。 If it is null, choose a default landing page -- probably something like "Your item has been removed, click here to continue shopping." 如果为空,请选择默认目标网页 - 可能类似于“您的项目已被删除,请点击此处继续购物”。 This latter will probably only rarely get hit. 后者很可能很少受到打击。

我从来没有尝试过,但您可以使用Referer标头来了解post或get的来源,并尝试将URL与路由匹配。

Just use the URL Referer [sic] header. 只需使用URL Referer [sic]标头即可。

var requestFrom = Request.UrlReferrer

You can find the documentation at: http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx 您可以在以下网址找到该文档: http//msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx

The only time this wouldn't work is when the page is requested directly, but in that case you wouldn't have any place to redirect to anyways. 这是不可行的唯一一次是直接请求页面,但在这种情况下,您无论如何都没有任何地方可以重定向。

There is also the option of doing the only thing async using AJAX, so that your Delete action only does what it describes and isn't responsible for doing something outside of its intended purpose of deleting. 还可以选择使用AJAX执行唯一的异步操作,以便您的Delete操作仅执行其描述的操作,并且不负责执行其预期删除目的之外的操作。

If you are using WCSF (Web Client Software Factory) to implement MVC pattern, you could use PageFlow to do all navigation. 如果您使用WCSF(Web客户端软件工厂)来实现MVC模式,则可以使用PageFlow执行所有导航。

Eg:- 例如:-

PageFlow.Next(); PageFlow.Next(); or PageFlow.Previous(); 或PageFlow.Previous();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM