Ali Shahzad
Ali Shahzad

Reputation: 5332

How to get the action name from which the user redirected to the current action in MVC4

I have two actions named 'abc' and 'def' in Home Controller. The action 'abc' returns RedirectToAction("def"). So can I get the name of the action in 'def' from which the Redirection triggered.

public ActionResult abc()
{
    //Some code
    return RedirectToAction("def");
}

public ActionResult def()
{
    //Some code
    string str = "You have been redirected from action 'abc'";
    return Content(str);
}

How can I get this name abc in the action def ?

Upvotes: 3

Views: 1451

Answers (2)

Chamaququm
Chamaququm

Reputation: 6728

You should not use URLReferrer. Why?

Because Request.UrlReferrer it is not 100% correct way as there are many ways that URLReferrer can be blocked by security software/Antivirus, Firewall, Proxy program. URLReferrer will be null in Pop Ups. That's the reason I can't use URLReferrer to check from where this page is opened up.

So, What's the best way ?

Request.RequestContext.RouteData.Values["action"]

Upvotes: 1

Daniele
Daniele

Reputation: 1938

You can use Request.UrlReferrer to get the referring URL or the Request.ServerVariables dictionary, something like Request.ServerVariables["http_referer"]

Upvotes: 1

Related Questions