ASP.net URL重写基于关闭查询字符串编号 [英] ASP.net URL rewrite based off query string ID
问题描述
大家好,我试图找出如何重新写一个URL,例如www.mywebsit.com/Articles/newsInfo.aspx?id=3到www.mywebisite.com/Articles/My-News-Title.aspx
Hey everyone I am trying to figure out how to re write a url such as www.mywebsit.com/Articles/newsInfo.aspx?id=3 to www.mywebisite.com/Articles/My-News-Title.aspx
我不知道如果我误解的东西,但下面是code我已经和似乎没有任何发生。该URL保持不变。
I don't know if I am misunderstanding something but below is the code i have and nothing seems to be happening. The URL stays the same.
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim currPath As String = Request.Url.ToString
If currPath.IndexOf("Articles") <> -1 And currPath.IndexOf("?np=") <> -1 Then
currPath = currPath.Substring(currPath.IndexOf("?np=") + 4)
Dim connectionString As String = ConfigurationManager.ConnectionStrings("MyConString").ConnectionString
Dim myreader As System.Data.SqlClient.SqlDataReader
Using myConnection As New System.Data.SqlClient.SqlConnection(connectionString)
Try
myConnection.Open()
Dim myCommand As New System.Data.SqlClient.SqlCommand()
myCommand.CommandType = Data.CommandType.Text
myCommand.CommandText = "SELECT TheNewsTitle FROM TheNews WHERE TheNewsId=" & currPath.Replace("'", "''")
myCommand.Connection = myConnection
myreader = myCommand.ExecuteReader
If myreader.HasRows Then
myreader.Read()
HttpContext.Current.RewritePath("~/Articles/" & myreader.GetValue(0).ToString.Replace(" ", "-") & ".aspx")
myreader.Close()
End If
myConnection.Close()
myCommand.Dispose()
myConnection.Dispose()
Catch ex As Exception
myConnection.Close()
myConnection.Dispose()
End Try
End Using
End If
End Sub
在code从不出错了,所以我不明白是怎么回事。
The code never errors out so i don't understand what is going on.
推荐答案
您想要做实际的URL重写?换句话说 - 你想在你的网站的链接,看起来像
Are you trying to do actual URL Rewriting ? In other words - you want the links in your site to look like
www.mywebsite.com/Articles/My-News-Title.aspx
www.mywebsite.com/Articles/My-News-Title.aspx
和
www.mywebsite.com/Articles/newsInfo.aspx?id=3
www.mywebsite.com/Articles/newsInfo.aspx?id=3
是?
看看这里: -
http://msdn.microsoft.com/en-us/library/ cc668201.aspx
http://msdn.microsoft.com/en-us/library/ dd329551.aspx
这篇关于ASP.net URL重写基于关闭查询字符串编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!