Remove HTML or ASPX Extension
Another simplest solution for achieving the same:
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");
}
}
If you have dynamic code, I think that the easiest thing to do is to just rename the files from .aspx to .html especially if you only have a handful of pages. There is no simple way to do it without rewriting the URL somehow.
However, with IIS 7, you can set it up really easily with an HTTP Module. Scott Guthrie explains this really well. In this post, he shows several approaches to customizing the URLs. I think that you would like approach #3 the best.
http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx
Another little bit more modern way to do this is using Microsoft.AspNet.FriendlyUrls. In the Global.asax.cs add:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(RouteTable.Routes);
and in the RouteConfig file
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
I ended up using the following sites:
http://blogs.msdn.com/b/carlosag/archive/2008/09/02/iis7urlrewriteseo.aspx
and
http://forums.iis.net/t/1162450.aspx
or basically the following code in my web.config file using the IIS7 URL Rewrite Module that most hosted sites now offer (in this case I am using GoDaddy):
<system.webServer>
<rewrite>
<rules>
<rule name="RewriteASPX">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="{R:1}.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>