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:

Related articles

March 10, 2015 21:41 in

Entity Framework performance tips

To honor the name of this blog I will also be writing posts about software development. The software developer that hasn’t turned to Google for some advice once in a while hasn’t been born yet. As a software developer you cannot know all the ins and outs of your trade. It might sound harsh but some people […]

March 19, 2015 17:14 in

Uninstalling a stubborn Windows 8.1 app

Today we came across the issue that one of our customers’ IT specialists received a strange message while trying to install a new build of one of our apps.

March 24, 2015 19:22 in

NuG(g)et: MiniProfiler

When using Entity Framework (or any other ORM for that matter) you always have to be aware of the queries you send to the database. When using a feature like lazy loading it’s fairly easy to create an N+1 query problem which makes you send more queries to the database than you should. MiniProfiler could help you out in […]