Question

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.

Was it helpful?

Solution

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

OTHER TIPS

Request.ServerVariables["http_referer"]

Should do.

You can use this

filterContext.RequestContext.HttpContext.Request.UrlReferrer.AbsolutePath

To correct use reffer url you should pass it to viewModel, 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(_ => _.Referrer);
    var referrer = HttpContext.Current.Request.UrlReferrer.AbsoluteUri;
    return new MvcHtmlString(str.ToHtmlString().Replace("value=\"\"", String.Format("value=\"{0}\"", referrer)));
}

...

@Html.HiddenForReferrer()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top