Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Get current controller and action name in helper

I'm trying to create custom html helper that will retrieve some text from XML file and render it on the view. XML is organized in hierarchy where top nodes represent controller names, following the action names and then individual keys.

Goal is to accomplish syntax such as:

@Html.Show("Title")

Where helper would infer controller name and action name from the view where it was called.

Is there a way to get that information in the html helper extension method?

like image 349
Admir Tuzović Avatar asked Oct 31 '12 06:10

Admir Tuzović


People also ask

What is ControllerContext?

ControllerContext(HttpContextBase, RouteData, ControllerBase) Initializes a new instance of the ControllerContext class by using the specified HTTP context, URL route data, and controller.


1 Answers

Even simpler:

htmlHelper.ViewContext.RouteData.Values["controller"] 

and

htmlHelper.ViewContext.RouteData.Values["action"] 

gives you the controller and action name, respectively.

like image 175
David R Avatar answered Dec 11 '22 08:12

David R