Remove HTML or ASPX Extension in IIS

In a hosted IIS environment, I am looking for the simplest way to use extension-less file names. Simply I have the following pages:

index.html (or .aspx) –> domain.com gallery.html –> domain.com/gallery videos.html –> domain.com/videos etc…

Option 1 – URL Rewrite

Use the following code in you web.config file using the IIS7 URL Rewrite Module. For more information on the URL Rewrite Module please check: http://www.iis.net/downloads/microsoft/url-rewrite


       
           // change to html for html pages
          
          
          
        
         
          
          
            
            
             // makes sure it only processes pages you specify in last rule
          
           
         
    

 

Option 2 – Global.ascx Edit

Put following lines of code into your global.ascx file:

void Application_BeginRequest(object sender, EventArgs e)
{
   String fullOrigionalpath = Request.Url.ToString();
   String[] sElements = fullOrigionalpath.Split('/');
   String[] sFilePath = sElements[sElements.Length - 1].Split('.');

   if (!fullOrigionalpath.Contains(".aspx") && sFilePath.Length == 1)
   {
       if (!string.IsNullOrEmpty(sFilePath[0].Trim()))
           Context.RewritePath(sFilePath[0] + ".aspx");
   }
}