|
|
jearol
发表于 2008-11-17 13:29:50
|
显示全部楼层
上代码:
在Global.asax里添加Application_BeginRequest事件,在请求web地址时运行
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim oldUrl As String = HttpContext.Current.Request.RawUrl
pattern = "^(.+)list,(\d+)\.aspx(\?.*)*$"
replace = "$1list.aspx?id=$2"
If Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase) Or RegexOptions.Compiled Then
Dim newurl As String = Regex.Replace(oldUrl, pattern, replace, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
Context.RewritePath(newurl)
End If
End Sub
这样就可以实现 list,1.aspx 到 list.aspx?id=1 这样的重定向,如果需要实现的地址和参数比较多,那么就需要一个判断,根据不同的地址来重定向到不同的页面,看看我网站上的代码:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim oldUrl As String = HttpContext.Current.Request.RawUrl
If InStr(oldUrl, ",") > 0 Then
Dim pattern As String = ""
Dim replace As String = ""
Select Case Right(Left(oldUrl, InStr(oldUrl, ",") - 1), Len(Left(oldUrl, InStr(oldUrl, ",") - 1)) - InStrRev(Left(oldUrl, InStr(oldUrl, ",") - 1), "/"))
Case "tech"
pattern = "^(.+)tech,(\d+)\.aspx(\?.*)*$"
replace = "$1tech.aspx?typeid=$2"
Case "techbrowse"
pattern = "^(.+)techbrowse,(\d+)\.aspx(\?.*)*$"
replace = "$1techbrowse.aspx?id=$2"
Case "newsbrowse"
pattern = "^(.+)newsbrowse,(\d+)\.aspx(\?.*)*$"
replace = "$1newsbrowse.aspx?id=$2"
Case "down"
pattern = "^(.+)down,(\d+)\.aspx(\?.*)*$"
replace = "$1down.aspx?id=$2"
End Select
If pattern.Length > 0 And replace.Length > 0 Then
If Regex.IsMatch(oldUrl, pattern, RegexOptions.IgnoreCase) Or RegexOptions.Compiled Then
Dim newurl As String = Regex.Replace(oldUrl, pattern, replace, RegexOptions.Compiled Or RegexOptions.IgnoreCase)
Context.RewritePath(newurl)
End If
End If
End If
End Sub
对于list.aspx?id1=1&id2=2&id3=3这样的地址,只需要写相应的正则表达式就ok了 |
评分
-
查看全部评分
|