Getting the name of the current controller and action in ASP.NET MVC

Aug 15, 2010 · Follow on Twitter and Mastodon dotnet

I have been trying get the name of the current controller and action in ASP.NET MVC, and have found a nice way to do so. Let’s see how to do this.

Although it’s not that nicely implemented in the framework, you can easily find this info by inspecting the ValueProvider like this:

string controllername = ValueProvider.GetValue("controller").RawValue.ToString();
string actionname = ValueProvider.GetValue("action").RawValue.ToString();

It’s not type safe and may change in future versions of ASP.NET MVC, but it works for now. Just keep an eye out for future changes.

Discussions & More

Please share any ideas, feedback or comments you may have in the Disqus section below, or by replying on Twitter or Mastodon.

If you found this text interesting, make sure to follow me on Twitter and Mastodon for more content like this, and to be notified when new content is published.

If you like & want to support my work, please consider sponsoring me on GitHub Sponsors.