Improve ASP.NET SEO by using System.Web.Routing

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.” — wikipedia.org Search Engine Friendly URLs vs “Dirty” URLs 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 maintain. In the example above you can immediately understand what the resource is about, while the RAW version is difficult to read and almost impossible to remember. ...

January 18, 2010 · 3 min · Andrea Azzola

Issues With AJAX and a Custom HttpModule

My web app needs to catch every non‑existent path as a search string, because I’m implementing an HttpModule that handles URLs in an SEO‑friendly way, e.g.: http://contoso.com/search-string But the Microsoft AJAX ScriptModule interfered, causing runtime errors like “AJAX Framework failed to load…”. The fix was to register my module after the ScriptModule and selectively bypass AJAX requests. <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, ..." /> <add name="MyModule" type="MyModule" /> using System; using System.Linq; using System.Web; public class MyModule : IHttpModule { public void Init(HttpApplication context) { context.PostResolveRequestCache += Application_OnAfterProcess; } private void Application_OnAfterProcess(object source, EventArgs e) { var application = (HttpApplication)source; var context = application.Context; if (context.Request.Headers["x-microsoftajax"] == null) { if (!System.IO.File.Exists(application.Request.PhysicalPath) && !application.Request.Url.ToString().Contains(".axd") && !application.Request.Url.ToString().Contains(".asmx")) { string newUrl = "~/Search.aspx?q=" + context.Server.UrlEncode(application.Request.Url.Segments.Last()); // other logic here… context.RewritePath(newUrl); } } } public void Dispose() { } } This runs at PostResolveRequestCache and, when the special x-microsoftajax header is absent, it rewrites unknown paths to your search page while letting AJAX handlers (*.axd, *.asmx) pass through normally. ...

June 18, 2005 · 1 min · Andrea Azzola