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)

2
  • 1
    Where do you want to use this data? If you want to display it in your view, then you already know which View that "created that page".
    – Dismissile
    Jan 30, 2012 at 15:15
  • @Dismissile I'll be placing this within my site.master page Jan 30, 2012 at 15:49

1 Answer 1

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.

3
  • 2
    Alternately: ViewContext.RouteData.Values["controller"] and ViewContext.RouteData.Values["action"]
    – Dismissile
    Jan 30, 2012 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, 2012 at 15:19
  • @Dismissile yes, you're right. I didn't realize OP needed the view as well. Updated my post accordingly. Jan 30, 2012 at 15:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.