Posts tagged ASP.NET

A simple redirect route handler for ASP.NET 3.5 routing

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.

ASP.NET MVC SEO – Solution Part 1

In a previous post I explained some of the issues with ASP.NET MVC when trying to implement an SEO-optimized web site.  In this post I’ll begin to explore some possible solutions.

Step 1: Master View – some additions

First let’s make it easy to set the meta description, page title, meta keywords and canonical url by adding the following to the head section of the master view:

<head id="Head1" runat="server">

    <title><%=ViewData["PageTitle"]%></title>  <%-- This gets wrapped here, so it sees a title tag and doesn't emit two --%>
    <%=ViewData["PageDescription"]%>           <%-- These are wrapped elsewhere so they vanish if not set--%>
    <%=ViewData["PageKeywords"]%>              <%-- These are wrapped elsewhere so they vanish if not set--%>
    <%=ViewData["CanonicalUrl"]%>              <%-- These are wrapped elsewhere so they vanish if not set--%>

    <meta name="robots" content="noodp" />  <%--Don't use Open Directory Project descriptions--%>

Note how we are not wrapping the canonical URL, and meta tags around the ViewData here (even though it would be more correct to do so). We do this so that when those tags are not present the entire tag disappears from the page instead of rendering a tag with an empty string in it. For the title tag however, that’s universal so let’s do it ‘properly’.

In tomorrow’s post I’ll show how we can set the Canonical URL using an attribute.

ASP.NET MVC meet SEO; SEO meet ASP.NET MVC

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!

ASP.NET Custom Validation

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.customvalidator.aspx

How annoying, “If the input control is empty, no validation functions are called and validation succeeds. Use a RequiredFieldValidator control to require the user to enter data in the input control. ”

The missing parameter you need to set is ValidateEmptyText=”true”.

Wasted a good half hour finding that little oddity.