If we create the Asp.Net web application the URL will contain the page extension ".aspx". For normal web applications it is fine to have page extension, but if we are creating the web application for online business we should take care of URL also. For that we have to rewrite the URL for our web application URL's.
We can rewrite the URLS in Asp.Net by creating the HttpModule which handles the URL whenever it requires for particular request as shown below.
using System;
using System.Web;
namespace AspNetURLRewriting
{
public class UrlRewritingModule : System.Web.UI.Page, IHttpModule
{
public UrlRewritingModule()
{
}
public string ModuleName
{
get
{
return "UrlRewritingModule";
}
}
public new void Init(HttpApplication application)
{
application.AuthorizeRequest += new EventHandler(application_AuthorizeRequest);
application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
}
private void application_PreRequestHandlerExecute(object sender, EventArgs e)
{
}
private void application_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication httpApp = sender as HttpApplication;
string sVirtualPath = string.Empty;
string sQueryString = string.Empty;
MapSEOFriendlyUrl(httpApp.Context, out sVirtualPath, out sQueryString);
if (sVirtualPath.Length > 0)
{
httpApp.Context.RewritePath(sVirtualPath, string.Empty, sQueryString);
}
}
private void MapSEOFriendlyUrl(HttpContext context, out string sVirtualPath, out string sQueryString)
{
sVirtualPath = string.Empty; sQueryString = string.Empty;
string sOriginalPath = context.Request.Path.ToLower();
if (sOriginalPath.IndexOf("urlrewrite") >= 0)
{
sVirtualPath = "~/seofriendly.aspx";
sQueryString = string.Empty;
}
}
}
}
As shown above we created class UrlRewritingModule.cs which implements the IHttpModule. In application_AuthorizeRequest event we handles the URL rewriting and we have to register application_AuthorizeRequest event in Init() event of Http Module. We have to register this UrlRewritingModule http module in Global.asax file as shown below.
using System;
using System.Web;
namespace AspNetURLRewriting
{
public class Global : System.Web.HttpApplication
{
public static IHttpModule Module = new UrlRewritingModule();
///<summary>
/// Initiates UrlRewritingModule http module
///</summary>
public override void Init()
{
base.Init();
Module.Init(this);
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
}
}
Now we have to test our URL rewriting module by having Asp.Net web pages. As shown below we have default.aspx page which has URL for seofriendly.aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="AspNetURLRewriting._default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="urlrewrite">Click for URL Rewrite</a>
</div>
</form>
</body>
</html>
As shown above we have URL as "urlrewrite", but in UrlRewritingModule.cs http module we are redirecting to seofriendly.aspx page.