Welcome to WebmasterWorld Guest from 3.209.80.87

Forum Moderators: ocean10000

Message Too Old, No Replies

Search Engine Friendly URLs in Global.asax

     
10:35 pm on Jan 20, 2010 (gmt 0)

New User

10+ Year Member

joined:May 2, 2007
posts: 17
votes: 0


I'm new to ASP.net and I'm just trying to get SE friendly urls working. I have the code almost there but it says the new path is not valid. I've checked the path and it is valid so I must be doing something wrong. I think I've taken it as far as I can without help.
The line it has a problem with is:

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]

6:21 am on Jan 21, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:733
votes: 0


Using a virtual path should do the trick:
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]

6:29 am on Jan 21, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:733
votes: 0


I saw this too late, your 'Invalid Path' message may not be what the actual exception is, so instead of
System.Diagnostics.Debug.WriteLine("Invalid path")

I would recommend:
System.Diagnostics.Debug.WriteLine(ex.Message)
4:02 pm on Jan 21, 2010 (gmt 0)

New User

10+ Year Member

joined:May 2, 2007
posts:17
votes: 0


The code is working now with the ~/ . Thanks for the tip about the exception.

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
8:50 am on Jan 22, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
posts:733
votes: 0


I prefer working with Lists instead of Arrays, here is an example:

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