Arnold Zahrneinder
Arnold Zahrneinder

Reputation: 5200

MVC 5: Obtaining the Refering URL

The controller class in MVC 5 apparently does not implement the Request.UrlReferrer as this property is always null and the VS says

When overridden in a derived class, get's information about the referrer URL ...

I check the ServerVariable and the Headers properties by serializing them as XML files to explore their contents and I did not found any key that returns the referring URL.

I already know how to manually handle this for example by TempData, Keeping the URL in session, ActionFilterAttribute for that. I'm not after any of these.

I simply wanna know if such behavior is implemented in MVC 5 by default and if so where I can find it.

The answers in other StackOverFlow posts are outdated

Upvotes: 1

Views: 930

Answers (1)

Shyju
Shyju

Reputation: 218892

Request.UrlReferrer will give you a Uri object when you are visiting the current page from a link in another web page. If you are directly accessing the url ( as you do when you hit F5 button in Visual studio), you will get a null value as the return value of Request.UrlReferrer call as there we are directly going to this page.

To verify this you can do this.

Have 2 action method

public ActionResult Index()
{
   var r = Request.UrlReferrer;
   return View();
}
public ActionResult About()
{ 
   return View();
}

Now in the about view(~/Views/Home/About.cshtml), add this makrup to generate a link to your index action.

@Html.ActionLink("Index","Index","Home")

Put a breakpoint in the Index action so you can inspect the r varibale value. Run your app. Go to About page, Click on your Index link and see what value you get in the r variable when the breakpoint hits the Index action.

Upvotes: 1

Related Questions