I have a lot of Web Forms knowledge, I mean a lot … and while no one will say Web Forms is dead, I think it would be true to say that MVC has proven to be a consistently better pattern for general web development. I have been hinting recently that I am working on the DasBlog software (which uses Web Froms) as I would like to ensure it continues to be viable and useable into the near future. However, until very recently the idea of updating DasBlog necessitated a plan to essentially start over and frankly that was out of the question.
In fact a few months back I tried integration with .NET Standard 1.6 (ASP.NET Web Core) but almost everything failed immediately and catastrophically, I did not even get a chance to hit a line of code. However, with the release of Standard version 2.0 I was able to get things up and running immediately. To be clear I could have just settled for upgrading the .NET version to vNEXT but I was more interested in moving in a more platform agnostic direction.
I need to do more thorough testing of course but initially it looks .NET Standard 2 covers code developed for DasBlog which goes back as far as 2003! That is pretty amazing API coverage!
Regardless of the API coverage, there are some very practical and historic problems that I have to address, the first is that almost every URL on my site ends with aspx, unnecessarily exposing the platform I am using, and while one could argue that the SEO is great, the URLs are objectively ugly.
In this post I wanted to look at one way to solve the problem of redirecting relatively static pages to more modern MVC ones. Now in ASP.NET Core there is a fascinating piece of middleware called ADDIISUrlRewrite. It allows me to redirect (30x) my old aspx pages to my new core MVC pages before getting to the MVC pipeline, here are three examples I want to change and what I want to redirect to:-
To accomplish this I updated my ASP.NET Web Core to include the Microsoft.AspNetCore.Design Nuget package, then in the Startup.cs added the following namespace:
Then in the Configure method (used to make changes to the the HTTP request pipeline) I add the following:
var options = new RewriteOptions() .AddIISUrlRewrite(env.ContentRootFileProvider, @"Config\DasBlogIISUrlRewrite.xml"); app.UseRewriter(options);
The xml file (DasBlogIISUrlRewrite.xml) is a snippet of the rewrite settings normally found in the web.config, here is how I solved the three redirects I needed: