Forum Moderators: open
incoming.RewritePath("http://localhost:81/testapp/Default2.aspx?pageid=" + pageid)
(P.s. How do you do code blocks in the forums? This doesn't seem to work)
[codes]
<%@ Application Language="VB" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim incoming As HttpContext = HttpContext.Current
Dim oldpath As String = incoming.Request.Path.ToLower()
Dim pageid As String 'page id requested
'Regular expressions to grab the page id from the pageX.aspx
Dim pattern As String = "/page/(\d+)/"
Dim options As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace
Dim regex As Regex = New Regex(pattern, options)
Dim matches As MatchCollection = regex.Matches(oldpath)
If (matches.Count > 0) Then
System.Diagnostics.Debug.WriteLine(matches.Count)
'Extract the page id and send it to Process.aspx
pageid = matches(0).Groups(1).ToString()
'Dim gPath As String = Path.GetFileName("http://localhost:81/testapp/Default2.aspx?pageid=" + pageid)
Try
incoming.RewritePath("http://localhost:81/testapp/Default2.aspx?pageid=" + pageid)
Catch ex As HttpException
System.Diagnostics.Debug.WriteLine("Invalid path")
End Try
'System.Diagnostics.Debug.WriteLine(gPath)
Else
'Display path if it doesn’t containt pageX.aspx
System.Diagnostics.Debug.WriteLine("No Matches")
incoming.RewritePath(oldpath, False)
End If
End Sub
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs on application shutdown
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when an unhandled error occurs
End Sub
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a new session is started
End Sub
Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
' Code that runs when a session ends.
' Note: The Session_End event is raised only when the sessionstate mode
' is set to InProc in the Web.config file. If session mode is set to StateServer
' or SQLServer, the event is not raised.
End Sub
</script>
[/codes]
incoming.RewritePath("~/testapp/Default2.aspx?pageid=" + pageid)
You can use [ code] [/ code] blocks for your source code, but they make the font size tiny and for some reason a line break interferes with them. I usually use the [ quote] blocks for this
[edited by: marcel at 8:06 am (utc) on Jan. 21, 2010]
I've since tried to get the script working with arrays but without success. I've made the MatchCollection into an array but I get the error:
Error1Value of type 'System.Text.RegularExpressions.MatchCollection' cannot be converted to '1-dimensional array of System.Text.RegularExpressions.MatchCollection'.C:\asp_sites\test2\Global.asax2118C:\asp_sites\test2\
For the line: matches(0) = regex.Matches(oldpath)
Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim incoming As HttpContext = HttpContext.Current
Dim oldpath As String = incoming.Request.Path.ToLower()
Dim pageid As String 'page id requested
Dim options As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.IgnorePatternWhitespace
Dim pattern(10) As String
Dim regex As Regex
Dim matches(10)() As MatchCollection
pattern(0) = "/page/(\d+)(/?)"
pattern(1) = "/(\*)/(\*)(/?)" 'matches mysite.com/insurance/life-insurance/ for example.
regex = New Regex(pattern(0), options)
matches(0) = regex.Matches(oldpath)
If (matches(0).Count > 0) Then
pageid = matches(0).Groups(1).ToString()
incoming.RewritePath("~/Default2.aspx?pageid=" + pageid)
Else
'System.Diagnostics.Debug.WriteLine("No page or number")
incoming.RewritePath(oldpath, False)
End If
End Sub
Dim patternList As New List(Of String)patternList.Add("/page/(\d+)(/?)")
patternList.Add("/(\*)/(\*)(/?)")'matches mysite.com/insurance/life-insurance/ for example.regex = New Regex(patternList(0), options)
You can do the same for your MatchCollection:
Dim matchCollectionList As New List(Of MatchCollection)
Which you can then loop through if needed:
For Each Match As MatchCollection In matchCollectionList
' Do your stuff
Next