【问题标题】:ASP.net URL rewrite based off query string ID基于查询字符串 ID 的 ASP.net URL 重写
【发布时间】:2014-01-10 07:30:29
【问题描述】:

大家好,我想弄清楚如何将诸如 www.mywebsit.com/Articles/newsInfo.aspx?id=3 之类的 URL 重新写入 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

代码永远不会出错,所以我不明白发生了什么。

【问题讨论】:

    标签: asp.net url-rewriting


    【解决方案1】:

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

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

    并被处理

    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

    【讨论】:

    • 这些让我找到了正确的方向。我花了一段时间才弄清楚一些概念。非常感谢!
    猜你喜欢
    • 2012-03-08
    • 2020-01-25
    • 1970-01-01
    • 2016-12-13
    • 2019-09-11
    • 1970-01-01
    • 2012-11-02
    • 2011-08-02
    • 2012-01-18
    相关资源
    最近更新 更多