Friday, July 22, 2005

Make use of URL Rewriting in ASP.NET

URL rewriting is used to allow the Web users to access many URL’s which does not really exist in the Web Server’s file system. The benefit of that are at least the followings:

1. Use part of URL’s as the parameter (query string) to the application. For example, a Canadian web site which supports both French and English language can use the URL’s to decide what kind of messages the Web Application should show:

http://www.domain.com/en-CA/login.aspx will show an English Login Screen

http://www.domain.com/fr-CA/login.aspx will show a French Login Screen

Inside the server, there is only one URL: http://www.domain.com/login.aspx

2. Hide the aspx extension, and the actual file system structure from Web Site, such that people may not know this is a ASP.NET site,a nd the hacker may not be able to hack the site by playing around the URL’s.

Before ASP.NET, it is hard to achieve this, other than writing a custom ISAPI dll, which is hard. With ASP.NET, we can easily intercept the requester’s URL, parse and rearrange to the real internal URL with the parameters.

To implement URL rewrite, we will need to develop a HTTPModule and include that Module into the web.config file. Like:

<system.web>
<httpModules>

<add name="Localization" type="Localization.LocalizationHttpModule, Localization" />

</httpModules>

</system.web>

The Module itself we intercept each URL request, parse and redirect to the real URL. Please note this Redirect is not the HTTP browser redirect, so it does not take additional round trip between the browser and the server. It redirect internally. It is faster and secure.

Public Sub Init(ByVal context As HttpApplication) Implements IHttpModule.Init

AddHandler context.BeginRequest, AddressOf context_BeginRequest

End Sub

Private Sub context_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)

Dim request As HttpRequest = CType(sender, HttpApplication).Request

Dim context As HttpContext = CType(sender, HttpApplication).Context

Dim applicationPath As String = request.ApplicationPath

If applicationPath = "/" Then

applicationPath = String.Empty

End If

Dim requestPath As String = request.Url.AbsolutePath.Substring(applicationPath.Length)

LoadCulture(requestPath)

context.RewritePath(applicationPath + requestPath)

End Sub

For detals, please refer to the following URL:


http://www.codeproject.com/aspnet/LocalizedSamplePart2.asp

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home