Wednesday, May 28, 2008

Search Engine Optimization(SEO) and URL Rewriting using HTTPModules - Microsoft ASP.NET

Implement IHttpModule interface, which has Initialization(public void Init(HttpApplication context)) methods. In there we attach URL Rewrite method to AuthorizeRequest (for windows authorization)
Note:This is a simplified version. For more information please see references at the end of this post.
namespace Root.Client.SEOHandler
{
    public class SEOModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.AuthorizeRequest += new EventHandler(SEOModuleAuthorizeRequest);
        }
        public void SEOModuleAuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            /// 
            ///we must implement switches collection in CustomConfiguration as a property. 
            ///See complete description of how to implement a custom configuration 
            ///section in web.config please see 
            ///Adding new section to web.config - Microsoft ASP.NET
            ///
            List<Switch> switches =((CustomConfiguration) HttpContext.Current.Cache ["customConfiguration"]).Switches;
            for (int i = 0; i < switches.Count; i++)
            {
                string lookFor = string.Format("{0}/{1}",app.Context.Request.ApplicationPath,switches[i]);
                if(string.Compare(lookFor,app.Request.Path)==0)
                app.Context.RewritePath(
                string.Format("{0}/{1}","required_page.aspx", app.Context.Request.ApplicationPath);
            }
        }
    }
}

Register assembly in web.config

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <system.web>
    <httpModules>
      <add name="SEOModule" type="Root.Client.SEOHandler.SEOModule" />
    </httpModules>
  </system.web>
</configuration>



Reference: URL Rewriting in ASP.NET

1 comment:

sampath said...
This comment has been removed by a blog administrator.

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...