5

I can get the name of the current action and controller like this:

string controllername = this.ValueProvider.GetValue("controller").RawValue.ToString();
string actionname = this.ValueProvider.GetValue("action").RawValue.ToString();

And I can also get the referring URL with something like this:

string MyReferrer = Request.UrlReferrer.ToString();

But how can I get the names of previous action and controller in an MVC 2 controller?

CarenRose
  • 1,266
  • 1
  • 12
  • 24
eno
  • 51
  • 1
  • 1
  • 3
  • 1
    not sure if it matters to you but referrer can be forged. consider this if its a security issue. – Adam Tuliper Aug 19 '11 at 17:14
  • 1
    This might work for you: http://stackoverflow.com/questions/8830052/how-do-i-get-the-controller-and-action-names-from-the-referrer-uri – TestSubject Jul 15 '13 at 10:15

5 Answers5

14

This should work!

// Home is default controller
var controller = (Request.UrlReferrer.Segments.Skip(1).Take(1).SingleOrDefault() ?? "Home").Trim('/'); 

// Index is default action 
var action = (Request.UrlReferrer.Segments.Skip(2).Take(1).SingleOrDefault() ?? "Index").Trim('/'); 
Husni Salax
  • 1,968
  • 1
  • 19
  • 29
jwize
  • 4,230
  • 1
  • 33
  • 51
0

asp.net mvc does not provide this, due to stateless nature of http, but you can store this using session or cookie

Adeel
  • 19,075
  • 4
  • 46
  • 60
0

My first attempt would be to parse the previous path from the Referring URL in the request object.

coder77
  • 154
  • 1
  • 3
  • 1
    I can get the reffer url by something like : string MyReferrer = Request.UrlReferrer.ToString(); , but I do not know how can I extract the names from the url ? – eno Aug 17 '11 at 03:51
  • The simple way is to split the string on the "/" into an array and then access the slots with the controller and action name respectively. You can also use the AbsolutePath property (Request.UrlReferrer.AbsolutePath) to get just the part after the domain. Of course, the action may or may not be present at which point you need to test for that and assume "Index" or whatever your default action is. – coder77 Aug 17 '11 at 03:55
0

Store this value in tempdata OR look up the referring route (via the url) to get the actual route object, then look at its controller and action property. In order to get this you need to be able to look up a route from a URL. To do this, refer to Phil Haacks code (there are others as well) to look up a route. There are various methods listed at:

http://blogs.msdn.com/b/simonince/archive/2011/01/28/unit-testing-asp-net-mvc-routes.aspx

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
0

Consider using ASP.NET MVC TempData.

http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

It will give you persistence across the current request and the next one, so you can store there routing information, like the last action called.

Data in TempData will be release from memory after the next request has been processed (in opposite to the ASP.NET Session object, that will release memory on Session timeout or termination).

ivan loire
  • 721
  • 7
  • 9