ASP.MVC 6 razor tag helpers for more designer friendly view markup

I’m really liking a lot of the ASP.NET 5 stuff which is coming down the line. Cross platform capabilities are cool, but more relevant and ‘exciting’ to me is the out of the box IOC container which hopefully will result in one less dependency on Ninject, CastleWindsor etc. and dynamic development which means I can change server side code, save and just refresh the browser without restarting the debug session.

On the MVC side, version 6 introduces tag helpers which I really like too. These allow us to get the same viewModel binding benefits of HTML helpers by applying data attributes to HTML tags rather than using the C# HTML helpers directly via @Html.TextBoxFor, @Html.LabelFor etc.

Markup in my opinion becomes less server side like, less ASP.NET MVC specific and more cleaner and client-side like meaning it would be easier for designers to work more fully on the views. The below example is from Dave Paquette’s introduction article on tag helpers which I link to below.

Example differences in markup between html helper and tag helper
Example differences in markup between html helper and tag helper

You can see that by using tag helpers we can specify things like classes, styles and other attributes (such as angular and knockout markup) in the normal HTML5 way rather than via an anonymous type. The only MVC specific part is the addition of the asp-for attribute. Matt DeKrey’s stackoverflow answer on the difference between html and tag helpers really sums up the benefits well I feel. I’m really looking forward to using razor this way, I think it lowers another barrier to working with MVC. If your preference however is to continue with HTML helpers they are still available of course.

By far the best coverage of MVC 6 tag helpers I’ve found is by Dave Paquette who has a tonne of articles about all the different tag helpers and also how you can create your own custom ones too. Start of with his Cleaner Forms using Tag Helpers in MVC6 article which is really good. Mike Brind’s Introducing TagHelpers in ASP.NET MVC 6 is another good introduction to tag helpers but also explores how they actually work under the hood.

404s with ASP.MVC on IIS 7.5

I was getting page not found errors yesterday when trying to deploy an ASP.Net MVC website which of course worked fine in Visual Studio to IIS 7.5. I tried a couple of things to resolve based on what I had read online including:

  • ensuring appPool was set to pipeline mode of Integrated and targeted towards v4.0
  • ensuring
    <system.webServer>
    <modules  runAllManagedModulesForAllRequests=”true“>
    <validation validateIntegratedModeConfiguration=”false”/>
    </system.webServer>
  • re-registering the .Net framework 4.0 with the aspnet_regiis tool to get all the latest HTTP handlers.

I believe the first two might be required but it was running the aspnet_regiis tool which done the trick for me. As highlighted below, you need to run this as an administrator. Right click on cmd.exe and select ‘Run as administrator’ and then navigate to the .Net framework 4.0 install directory to run the tool.

reregisterNetFramework

 

After you’ve registered 4.0 with IIS you should now see much more HTTP handlers in your IIS configuration manager. The most important one I believe (but I didn’t disable to confirm) in this instance is ExtensionlessUrlHandler-Integrated-4.0

httphanders

404’s disappeared for me after that. Of course during my research there seemed to be a lot of things which could have caused this problem but running aspnet_regiis is what worked for me. I think it might have been because I installed IIS after installing 4.0, but I’m not sure.