[RESOLVED]How to Get RouteData from Referring Controller/Action

Hi All,

Please excuse my Newbieness, but I need some help here.  I need to get the name of the Controllerthat calls an Action in another Controller.  I was using

Request.UrlReferrer.LocalPath

Which worked fine (I just stripped out the name), but I was told that I cannot use this because the UrlReferrer can be forged.  I was told that I had to use the "RouteData".  Can someone tell me how to do this? 

Any assistance is greatly appreciated!


(ControllerContext.RouteData.Values["Controller"];

BeeDev

I was told that I had to use the "RouteData

By default RouteData shows the current route and not the previous conotroller.

BeeDev

I was told that I cannot use this because the UrlReferrer can be forged

Yes UrlReferrer can be forged. But I believe you don’t have any other option than using UrlReferrer. If you want, you can protect your site from forgery using different techniques. Please take a look at this blog for more details

Preventive Method for URL Request Forgery- An Example with ASP.NET MVC

Thanks for the response, but this is giving me the "current" controller, which now that I think about it, is actually how(and where) the method is being called.  The problem is that the action is being called from a partial view on a view (its a footer that
navigates through the pages in the application).  I actually need the name of the PAGE(view) Controller which would be the parent at the time.  That’s why I was using the URLReferrer.

use viewbag properties to store controller name and pass that viewbag data to the partial view method 

If you want the parent action or controller, then use the following code

ControllerContext.ParentActionViewContext.RouteData.Values["action"] 

Thanks, this worked great!

Leave a Reply