基于查询字符串ID的ASP.net URL重写

基于查询字符串ID的ASP.net URL重写,asp.net,url-rewriting,Asp.net,Url Rewriting,大家好,我正在想办法重新编写一个url,比如www.mywebsit.com/Articles/newsInfo.aspx?id=3到www.mywebisite.com/Articles/My-News-Title.aspx 我不知道我是否误解了什么,但下面是我的代码,似乎什么都没有发生。URL保持不变 Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) Dim currPath As S

大家好,我正在想办法重新编写一个url,比如www.mywebsit.com/Articles/newsInfo.aspx?id=3到www.mywebisite.com/Articles/My-News-Title.aspx

我不知道我是否误解了什么,但下面是我的代码,似乎什么都没有发生。URL保持不变

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
子应用程序_BeginRequest(ByVal sender作为对象,ByVal e作为事件参数)
Dim currPath As String=Request.Url.ToString
如果currPath.IndexOf(“Articles”)-1和currPath.IndexOf(“?np=”)-1,则
currPath=currPath.Substring(currPath.IndexOf(“?np=”)+4)
Dim connectionString As String=ConfigurationManager.connectionString(“MyConString”).connectionString
将myreader设置为System.Data.SqlClient.SqlDataReader
将myConnection用作新的System.Data.SqlClient.SqlConnection(connectionString)
尝试
myConnection.Open()
将myCommand作为新System.Data.SqlClient.SqlCommand()进行修改
myCommand.CommandType=Data.CommandType.Text
myCommand.CommandText=“从新闻中选择新闻标题,其中新闻ID=“&currPath.Replace”(“,”)”
myCommand.Connection=myConnection
myreader=myCommand.ExecuteReader
如果myreader.HasRows那么
myreader.Read()
HttpContext.Current.RewritePath(“~/Articles/”&myreader.GetValue(0).ToString.Replace(“,”-”&“.aspx”)
myreader.Close()
如果结束
myConnection.Close()
myCommand.Dispose()
myConnection.Dispose()
特例
myConnection.Close()
myConnection.Dispose()
结束尝试
终端使用
如果结束
端接头

代码从未出错,所以我不明白发生了什么。

您是否正在尝试实际的URL重写?换句话说,您希望站点中的链接看起来像

www.mywebsite.com/Articles/My-News-Title.aspx

并由

www.mywebsite.com/Articles/newsInfo.aspx?id=3

是吗

请看这里:-


这些让我找到了正确的方向。我花了一段时间才弄明白一些概念。非常感谢!