如何在ASP中获取引用URL。净MVC行动?

时间:2022-11-30 22:42:45

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中获取引用URL。净MVC行动?在您调用操作之前,我尝试重定向回页面。

4 个解决方案

#1


125  

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

您可以使用请求。如果不喜欢访问请求,也可以使用UrlReferrer来获取引用URL。直接ServerVariables字典。

#2


21  

Request.ServerVariables["http_referer"]

Should do.

应该做的。

#3


6  

You can use this

你可以使用这个

filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath

#4


3  

To correct use reffer url you should pass it to viewModel, try so:

要正确使用reffer url,你应该把它传递给viewModel,试试:

public interface IReferrer
{
    String Referrer { get; set; }
}

...

public static MvcHtmlString HiddenForReferrer<TModel>(this HtmlHelper<TModel> htmlHelper) where TModel : IReferrer
{
    var str = htmlHelper.HiddenFor(_ => _.Referrer);
    var referrer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
    return new MvcHtmlString(str.ToHtmlString().Replace("value=\"\"", String.Format("value=\"{0}\"", referrer)));
}

...

@Html.HiddenForReferrer()

#1


125  

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

您可以使用请求。如果不喜欢访问请求,也可以使用UrlReferrer来获取引用URL。直接ServerVariables字典。

#2


21  

Request.ServerVariables["http_referer"]

Should do.

应该做的。

#3


6  

You can use this

你可以使用这个

filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath

#4


3  

To correct use reffer url you should pass it to viewModel, try so:

要正确使用reffer url,你应该把它传递给viewModel,试试:

public interface IReferrer
{
    String Referrer { get; set; }
}

...

public static MvcHtmlString HiddenForReferrer<TModel>(this HtmlHelper<TModel> htmlHelper) where TModel : IReferrer
{
    var str = htmlHelper.HiddenFor(_ => _.Referrer);
    var referrer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
    return new MvcHtmlString(str.ToHtmlString().Replace("value=\"\"", String.Format("value=\"{0}\"", referrer)));
}

...

@Html.HiddenForReferrer()