I uploaded the source code for this, and it is at http://planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=1204&lngWId=10 The method used to change the url is the RewritePath method. Let's begin with the tutorial.
Create a new webproject.
Add a class to the project. Name it Rewriter Add Imports statements for System and System.Web.
We will write 2 methods(procedures/functions)
The first will be a Private Function returning a String. This function will act as a translator for our URL.
Public Function GetSubstitution(ByVal zPath As String) As String
'This first string check is to see if the word test is in the URL 'If So, return show.aspx 'show.aspx is the actual page and zPath is what is displayed in the browser and passed to the server through links
If InStr(zPath, "test") > 0 Then
Return "show.aspx"
End If
'This second string validation checks the URL for the extension htm 'If it is, replace the extension with aspx
If InStr(zPath, ".htm") > 0 Then
Dim intMarker As Integer
Dim strTemp As String
intMarker = Len(zPath) - 4
strTemp = Mid(zPath, 1, intMarker)
Return strTemp & ".aspx"
End If
'The third check will show us how we can pass a query string '
If IsNumeric(Left(zPath,Len(zPath)-4)) = True Then
Dim strValue() As String
strValue = Left(zPath,Len(zPath)-4)
Return "show.aspx?ID=" & strValue
End If
'After all string checks were perforemed 'and none of the criteria didn't match 'Return the original string
Return zPath
End Function
The second will be a Public Sub Procedure and include the Shared statement (this will be called from our web project).
Public Shared Sub ReplaceURL()
Create an instance of the class
Dim objRewrite As ReWriter = New urlReWriter()
Create a string variable for our URL substitution
Dim strSubst As String
Set the substitution string to our first function GetSubstitution 'We will pass the URL being sent to the browser 'through the function to get it's replacement
strSubst = objRewrite.GetSubstitution(HttpContext.Current.Request.Path)
'If the length of our new URL is greater than zero 'Rewrite the URL path
If strSubst.Length > 0 Then
HttpContext.Current.RewritePath(strSubst)
End If
End Sub
Now in order to implement this, we need to call our class and put it to work. In the Application_BeginRequest of the Global.asax file, call the ReplaceURL function.
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
ReWriter.ReplaceURL()
End Sub
This won't work yet. There's one more thing we have to do. That is tell the webserver how to handle our extensions.
open the IIS management console. Open the properties of our webproject. Click configuration button on the directory tab. Add new extension In the executable page, enter C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll For extensions, enter the extensions you will be using, for this example, lets use .* Under Verbs, select Limit to, and enter GET,HEAD,POST Uncheck Check that file exists Click Ok, and apply settings. Now let's test it out. Add a web form to the project. Name it show.aspx, and another named 5.aspx In both pages in the page load add the line Response.Write("Page= " & Request.Url.ToString)
Now add an HTML page. Call it what you wish. Add links to 5.tst and text.htm and test.zzz
The ReWrite function will be called on request of the pages, and will tell the server which page to load.
|