MVC
A simple redirect route handler for ASP.NET 3.5 routing
Apr 20th
ASP.NET 3.5 Routing is a very powerful tool not just for registering routes for newer ASP.NET MVC applications but also for adding SEO friendly routes to older Webforms (ASPX) applications, or for routing multiple URLs to a single page. But that’s not all it can do. You can create your own IRouteHandler and then have complete control over what to do with any incoming HttpRequest.
Here for example is a way to do a permanent redirect when a given route is matched. To use it you might, for example, do:-
routes.Add(new Route("sample.aspx", new RedirectRouteHandler("/home/start")));
Here is the RedirectRouteHandler that can turn any request into a 301 redirect for you:-
/// <summary> /// Redirect Route Handler /// </summary> public class RedirectRouteHandler : IRouteHandler { private string newUrl; public RedirectRouteHandler(string newUrl) { this.newUrl = newUrl; } public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new RedirectHandler(newUrl); } } /// <summary> /// <para>Redirecting MVC handler</para> /// </summary> public class RedirectHandler : IHttpHandler { private string newUrl; public RedirectHandler(string newUrl) { this.newUrl = newUrl; } public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext httpContext) { httpContext.Response.Status = "301 Moved Permanently"; httpContext.Response.StatusCode = 301; httpContext.Response.AppendHeader("Location", newUrl); return; } }
Note: I’m not saying this is the best or only way to handle this. You’ll want to look at Url Rewriting and the Application and Request Routing module for IIS7 in particular.
How to stop IIS7 from handling 404 errors so you can handle them in ASP.NET
Apr 17th
IIS7 has lots of places you could look to make this change: you might start off looking to see if it’s an advanced option on your application pool, no, so then you try looking at the web site itself and the option .NET Error Pages. That has to be it, surely! So you try every option there Mode=On, Mode=Off, Mode=Remote Only. Nothing works so you consult the help for those items only learn that “Mode” is to “Select a mode for the error pages: On, Off, or Remote Only.” You can see now why help writers at Microsoft are so well paid – who would have guessed that Mode = Remote Only sets the Mode to Remote Only!
Now you are really frustrated but luckily you landed on my blog post here where you learned that the true path to 404 happiness is a simple change to your web.config:
<system.webServer> <httpErrors errorMode="Detailed" />
ASP.NET MVC meet SEO; SEO meet ASP.NET MVC
Mar 3rd
Whilst ASP.NET is clearly the best thing to hit .NET web development in a long-time it seems like the framework itself is somewhat challenged when it comes to SEO. For starters the concept of a page has all but disappeared – sure you can have a ViewPage but there’s no code associated with it. And sure, you have ASP.NET Routing so you can do anything you like with routes but the catchall route {Controller}/{Action}/{id} is as much a liability as it is a benefit as it catches things you really didn’t want it to catch and generates routes you really didn’t want to generate all too easily.
Convention over configuration is nice and all that, but sometimes a bit of configuration is necessary to bring your house into order, especially when the convention doesn’t allow things you really want for SEO.
So let’s take a look at all the things we really want to be able to do when creating an SEO friendly web site and see how we can get ASP.NET MVC to handle them.
For SEO we need:-
1. The ability to define a canonical url for a page. To use that canonical URL whenever we generate a route. To include that canonical url in the page header to instruct search engines that this is the canonical url for that page.
2. The ability to define multiple alternate URLs for a page. Plans change and your site changes too but you don’t want 404 errors, you want the user to land on the same page even if you changed the URL to improve its SEO keyword content for example. Ideally you’d like to 301 redirect these legacy urls but having them at least display the right page and including the canonical url in the header for that page is good enough.
3. The ability to use hyphens in urls. But since Controllers are classes and Actions are methods and the convention is to use them as parts of the URL this isn’t supported out of the box.
4. The ability to define title, meta description and meta keywords tags for a page in such a way that you can enforce rules around them such as requiring every public page to have a title tag, or ensuring that the length of the title tag is reasonable, or ensuring that your product name is on the end of every title tag.
5. The ability to build a sitemap.xml file that we can submit to Google or Bing containing every URL that we want them to index.
In my next few posts I’ll explain how we can overcome all of these shortcomings of ASP.NET MVC to create a great SEO-friendly web site.
Stay tuned!
Building sitemap.xml for SEO ASP.NET MVC
Feb 9th
Creating a sitemap.xml file for Google and other search engines can be accomplished in MVC using a simple ActionResult that returns the appropriate XML blog. The problem however is in generating the list of URLs to go into that sitemap.xml file.
In ASP.NET MVC there is no distinction between an action method that is a page and one that is a service call. To remedy that let’s create an ActionFilterAttribute that can be applied to any method to mark it as a page and to record the URL that we want that page to show up as in our sitemap:
1 /// <summary>
2 /// This attribute indicates that a method is an actual page and gives the data for it
3 /// </summary>
4 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
5 public class MVCUrlAttribute : ActionFilterAttribute
6 {
7 public string Url { get; private set; }
8
9 public MVCUrlAttribute(string url)
10 {
11 this.Url = url;
12 }
13
14 public override void OnResultExecuting(ResultExecutingContext filterContext)
15 {
16 string fullyQualifiedUrl = filterContext.HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + this.Url;
17 // We build HTML here because we want the View to be easily able to include it without any conditionals
18 // and because the ASP.NET WebForms view engine sometimes doesn’t subsitute <% in certain head items
19 filterContext.Controller.ViewData["CanonicalUrl"] = @”<link rel=”"canonical”" href=”"” + fullyQualifiedUrl + ” />”;
20 base.OnResultExecuting(filterContext);
21 }
22 }
You may wonder at this point why we can’t just use the Routing table to figure this out. The issue is that multiple routes may map onto one page but we still want it to show up just once in the sitemap otherwise we will get slammed for duplicate content. We also want to be able to mark each page with its canonical URL.
Now we can use reflection to find all of the ‘pages’ in our ASP.NET MVC Application and then build a sitemap.xml file from them.
1 List<string> allPageUrls = new List<string>();
2
3 // Find all the MVC Routes
4 Log.Debug(“*** FINDING ALL MVC ROUTES MARKED FOR INCLUSION IN SITEMAP”);
5 var allControllers = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.IsSubclassOf(typeof(Controller)));
6 Log.DebugFormat(“Found {0} controllers”, allControllers.Count());
7
8 foreach (var controllerType in allControllers)
9 {
10 var allPublicMethodsOnController = controllerType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
11 Log.DebugFormat(“Found {0} public methods on {1}”, allPublicMethodsOnController.Count(), controllerType.Name);
12
13 foreach (var publicMethod in allPublicMethodsOnController)
14 {
15 var mvcurlattr = publicMethod.GetCustomAttributes(true).OfType<MVCUrlAttribute>().FirstOrDefault();
16 if (mvcurlattr != null)
17 {
18 string url = mvcurlattr.Url;
19 Log.Debug(“Found “ + controllerType.Name + “.” + publicMethod.Name + ” <– “ + url);
20 allPageUrls.Add(url);
21 }
22 }
23 }
Issues combining ASP.NET MVC and ASP.NET Web Forms in the same application
Feb 6th
Recently I started migrating an ASP.NET WebForms project to ASP.NET MVC. Hoping to do this in phases I created a combined project that is both a webforms project and an MVC project.
Routing allows some requests to go to the WebForms pages and some to go to the new MVC pages.
Routing has also enabled SEO friendly URLs for all pages.
Everything seemed to be working great until I added a form to an MVC page and the URL it decided to use for posting the results back was one of the ASPX pages instead of the page it should have been. The issue was that routes added for the legacy ASPX pages were being picked up as matches for the route that was being requested.
This article was helpful in explaining how to configure routes to avoid this problem http://forums.asp.net/t/1484855.aspx but I still had an issue with the root “/” Route which was being picked for every form I created. So I changed the root route to this and now all is well. Of course, changing the home page to an MVC Action would also have solved the issue but I’m not ready to flip it just yet.
Route(“”,
new RouteValueDictionary(new { Controller = “Dummy”, Action = “Dummy” }),
new RouteValueDictionary(new { Controller = “Dummy”, Action = “Dummy” }),
routeHandler);