Getting route data in ASP.NET Razor

When developing a web app using ASP.NET MVC you sometimes come across to make decisions based on the current page. A good example of this is when you’re using Bootstrap and you want to add “active” classes to certain menu elements. You can find out where you are using the RouteData dictionary object. This contains the current area, action, controller among others. I personally use the extension method below to figure out where I am in my Razor pages. Some usage samples can also be found below.

 

 

 

 

 

 

 

 

And here’s the gist of the actual extension: