asp.net-mvc 如何在 ASP.NET MVC 操作中获取引用 URL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1471188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I get the referrer URL in an ASP.NET MVC action?
提问by Ryall
How do I get the referrer URL in an ASP.NET MVC action? I am trying to redirect back to the page before you called an action.
如何在 ASP.NET MVC 操作中获取引用 URL?我正在尝试在您调用操作之前重定向回页面。
回答by Derek Lawless
You can use Request.UrlReferrerto get the referring URL as well if you don't like accessing the Request.ServerVariablesdictionary directly.
Request.UrlReferrer如果您不喜欢Request.ServerVariables直接访问字典,也可以使用获取引用 URL 。
回答by Daniel Elliott
Request.ServerVariables["http_referer"]
Should do.
应该做。
回答by Navish Rampal
You can use this
你可以用这个
filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath
回答by Andrey Burykin
You can pass referrer url to viewModel, in my opinion it's better approach then sharing via the state, try so:
您可以将引用网址传递给 viewModel,在我看来,这是通过状态共享更好的方法,请尝试:
public interface IReferrer
{
String Referrer { get; set; }
}
...
...
public static MvcHtmlString HiddenForReferrer<TModel>(this HtmlHelper<TModel> htmlHelper) where TModel : IReferrer
{
var str = htmlHelper.HiddenFor(hh => hh.Referrer);
var referrer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
return new MvcHtmlString(str.ToHtmlString().Replace("value=\"\"", String.Format("value=\"{0}\"", referrer)));
}
...
...
@Html.HiddenForReferrer()

