January 2016

Getting route data in ASP.NET Razor

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.

Here’s the code for the extension:

Share this Post

Spread the word! Share this article on BlueSky or copy the link below to paste it anywhere — whether it's in your group chat, a carrier pigeon message, or a note in a bottle. The possibilities truly are endless.

Share on BlueSky