Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How do I output the Controller and View that is currently being rendered?

I have a complicated set of routes and I need to edit a specific web page. Given a URL, how do I determine which controller and view created that page?

I'm open to using ASP.NET MVC to write the information directly to the page where textcolor== background color, or anything else you may recommend.

I'd like a solution that I can use in production (where the MVC route debugger is disabled)

like image 811
makerofthings7 Avatar asked Nov 19 '25 04:11

makerofthings7


1 Answers

You can access the controller and action directly through the ViewContext.

// ASP.Net MVC 3
ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
ViewContext.Controller.ValueProvider.GetValue("action").RawValue

// ASP.Net MVC 2 and below:
ViewContext.Controller.ValueProvider["controller"].RawValue
ViewContext.Controller.ValueProvider["action"].RawValue

To get the view, have a look at this answer to a similar question by Phil Haack.

like image 85
Dennis Traub Avatar answered Nov 21 '25 19:11

Dennis Traub