如何在 ASP.NET MVC 操作中获得引用 URL?

如何在 ASP.NET MVC 操作中获得引用 URL?我试图重定向回到页面之前,你要求的行动。

91004 次浏览
Request.ServerVariables["http_referer"]

Should do.

You can use Request.UrlReferrer to get the referring URL as well if you don't like accessing the Request.ServerVariables dictionary directly.

You can use this

filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath

You can pass referrer url to viewModel, in my opinion it's better approach than sharing via the state, try so:

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()