Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
| 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
'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"
'The third check will show us how we can pass a query string
If IsNumeric(Left(zPath,Len(zPath)-4)) = True
Dim strValue() As String
strValue = Left(zPath,Len(zPath)-4)
Return "show.aspx?ID=" & strValue
'After all string checks were perforemed
'and none of the criteria didn't match
'Return the original string
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
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)
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
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.
Other 6 submission(s) by this author
|Report Bad Submission|
See Voting Log |
|Other User Comments|
|5/10/2003 1:31:37 PM:|
Very good idea! 5 from me!
I wonder if
you could compile it into DLL and load
it from "ISAPI Filters" tab.
|5/12/2003 9:52:38 AM:undercodex|
Well good example, can be used to write
a reverse proxy in example.
|5/15/2003 10:51:32 AM:Shane Bauer|
| Add Your Feedback! |
|Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name. |
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here.