Umschreiben der ASP.net-URL basierend auf der ID der Abfragezeichenfolge
Hallo allerseits, ich versuche herauszufinden, wie ich eine URL wie www.mywebsit.com/Articles/newsInfo.aspx?id=3 auf www.mywebisite.com/Articles/My-News-Title.aspx umschreiben kann
Ich weiß nicht, ob ich etwas missverstehe, aber unten ist der Code, den ich habe und nichts scheint zu passieren. Die URL bleibt gleich.
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
Der Code ist nie fehlerfrei, deshalb verstehe ich nicht, was los ist.