This page is specific to Microsoft Visual Studio 2008/.NET Framework 3.5 or higher
SEO and ASP.NET
"
As an Internet marketing strategy, SEO considers how search engines work and what people search for. Optimizing a website primarily involves editing its content and HTML indexing activities of search engines." [source:
wikipedia.org]
Search Engine Friendly URLs vs "Dirty" URLs
Current URL will be taken as example
- SEF URL
: http://AndreaAzzola.com/seo-asp-net-routing
- RAW URL
: http://AndreaAzzola.com/Post.aspx?id=cd171f7c-560d-4a62-8d65-16b87419a58c
SEFs are better to write, remember, understand and mantain. In the example above you can understand immediatly what the resource is about but the RAW version is difficult to read and almost impossible to rember.
Implementation
ASP.NET offers a great resource called 'System.Web.Routing', it support routes. Routes translates both the pattern you define and the context parameters, into an intelligible request. In the following example you will find:
- How to enable routing on your web application
- A class that handle routes by implementing IRouteHandler
- How to initialize routes when your application starts
- How to pass values in a clean and robust manner
#1 File Web.config
<system.web>
...
<add name="RoutingModule" type="System.Web.Routing.UrlRoutingModule" />
</system.web>
#2 File Global.asax
public class EnhancedRouteHandler : IRouteHandler
{
string _pageURL;
public EnhancedRouteHandler(string pageURL)
{
_pageURL = pageURL;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
IRoutePage page =
BuildManager.CreateInstanceFromVirtualPath(_pageURL,
typeof(IRoutePage)) as IRoutePage;
page.Parameters = requestContext.RouteData.Values;
return page;
}
}
#3 File Global.asax
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route(string.Empty,
new EnhancedRouteHandler("~/Default.aspx"))); // nothing after domain name
routes.Add(new Route("post/{slug}",
new EnhancedRouteHandler("~/Post.aspx"))); // post followed by his slug
routes.Add(new Route("twitter-mutuality",
new EnhancedRouteHandler("~/Apps/Mutuality.aspx"))); // a fixed path
routes.Add(new Route("{*catchall}",
new EnhancedRouteHandler("~/Error.aspx"))); // catchall
}
The catchall part works whenever the engine does not find a suitable resource or route for the request, this behaviour is extremely helpful expecially when dealing with 404 error or moved resources.
#4 File App_Code\IRoutePage.cs
using System.Web;
using System.Web.Routing;
public interface IRoutePage : IHttpHandler
{
RouteValueDictionary Parameters { get; set; }
}
By using this code you are obliged to inherit in each page you want to route to, and you should implement the Parameters field as well, however it doesn't require you to expose unappropriate querystrings or dangerous sessions that would damage the aesthetics of your code :D. Be aware, this won't cover all the possible test cases, but it would help with the most of them.
In Conclusion
In recent years we have witnessed a turnaround in the development of web-applications, especially since the technology made it possible to enlarge the catchment area, just think about mobile, MID/Netbook sales growth, disadvantage people... this has helped to promote a web simple and functional at the expense of a powerful web, but less usable.
SEFs are an important way to catch the guest's attention, please consider for example the Google' results page, search anything you want, and think about the link you'll choose, the clean one, or the dirty one? Not all clean URLs are the most attractives yet, but there are a good changes you will choose the simple one, this would mean an important point for your SEO score.