你如何重定向到ASP.NET MVC调用页面? [英] How do you redirect to the calling page in ASP.NET MVC?

查看:114
本文介绍了你如何重定向到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆