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

Aug 15, 2010 · Follow on Twitter and Mastodon dotnet

I’ve been trying to find out how to get the name of the current controller and action in ASP.NET MVC. If you’re curious on how to do it, do read on.

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.