3

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)

makerofthings7
  • 60,103
  • 53
  • 215
  • 448

1 Answers1

6

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.

Community
  • 1
  • 1
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • 2
    Alternately: ViewContext.RouteData.Values["controller"] and ViewContext.RouteData.Values["action"] – Dismissile Jan 30 '12 at 15:16
  • OP asked to know the Controller and View that created the page...I'm not sure if this is a typo and he wanted Controller and Action or if he needs to know the View as well. – Dismissile Jan 30 '12 at 15:19
  • @Dismissile yes, you're right. I didn't realize OP needed the view as well. Updated my post accordingly. – Dennis Traub Jan 30 '12 at 15:23