【发布时间】:2015-09-01 03:36:04
【问题描述】:
我正在为工作中的网站创建一个新页面,我的老板希望它有一个友好的 URL。
目前是website.com/account-payments.aspx,他希望它是website.com/payments/
问题是,我不知道如何正确重写 url。我看到一个 NuGet 包推荐了“Microsoft ASP.NET 友好 URL”,但我在这个网络上没有管理员权限,我无法安装任何新的东西。我的老板在这方面帮助不大。
ASP.net 是否有任何内置功能可以让我重写 url?
编辑
我已将此类添加到我的 App_Code 文件夹中。
Imports Microsoft.VisualBasic
Imports System.Web
Public Class PaymentsHandler
Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As _
System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
If context.Request.RawUrl.EndsWith("/payments/") Then
context.Server.TransferRequest("/account-payments.aspx")
End If
End Sub
Public ReadOnly Property IsReusable As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
在我的 web.config 文件中使用以下处理程序:
<handlers>
<add name="Payments" verb="*" path="/payments" type="PaymentsHandler" resourceType="Unspecified" />
</handlers>
现在,我在尝试浏览 website.com/payments/ 时收到 500 内部服务器错误,而不是一般的 404 错误
编辑 2:Electric Boogaloo
所以在 Googlin 之后,我发现有人遇到了类似的问题,他们通过在 web.config 中添加一个额外的处理程序来解决他们的问题。
<httpHandlers>
<add verb="*" path="*/account-payments.aspx" type="PaymentsHandler" validate="false"/> </httphandlers>
我检查了我的 webconfig,果然<handlers> 和 <httpHandlers> 中都有现有的处理程序
现在,我收到的是 403 错误,而不是旧的 500 错误。真的很困惑。
编辑 3:编辑
进步!我从 HTTPHandler 切换到 HTTPModule 并安装了我的 PaymentsHandler.vb 以匹配:
Public Class PaymentsHandler
Implements IHttpModule
Public Sub Init(context As HttpApplication) Implements System.Web.IHttpModule.Init
AddHandler context.BeginRequest, New EventHandler(AddressOf context_BeginRequest)
AddHandler context.EndRequest, New EventHandler(AddressOf context_EndRequest)
AddHandler context.AuthorizeRequest, New EventHandler(AddressOf context_AuthorizeRequest)
End Sub
Private Sub context_AuthorizeRequest(sender As Object, e As EventArgs)
'We change uri for invoking correct handler
Dim context As HttpContext = DirectCast(sender, HttpApplication).Context
If context.Request.RawUrl.Contains("account-payments.aspx") Then
Dim url As String = context.Request.RawUrl.Replace("account-payments.aspx", "payments")
context.RewritePath(url)
End If
End Sub
Private Sub context_EndRequest(sender As Object, e As EventArgs)
'We processed the request
End Sub
Private Sub context_BeginRequest(sender As Object, e As EventArgs)
'We received a request, so we save the original URL here
Dim context As HttpContext = DirectCast(sender, HttpApplication).Context
If context.Request.RawUrl.Contains("account-payments.aspx") Then
context.Items("originalUrl") = context.Request.RawUrl
End If
End Sub
Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
End Sub
现在我可以看到正在重写!耶!因此,当我浏览到 website/account-payments.aspx 时,它会自动转到 website/payments/ 并且 然后 我收到 403 - Forbidden 错误。
所以它正在重写和重定向......但显然该页面现在被禁止了?
【问题讨论】:
-
您应该浏览到
website/payments,它会重定向到website/account-payments.aspx。您需要在代码中切换这些回合。 403是因为它认为你想查看payments目录,不允许列出内容。 -
我在现有的两个带有 .aspx 扩展名的页面上进行了尝试,你是对的。但是,一旦我将其更改回无扩展的
/payments页面,我就会再次收到 403 错误。如果没有 .aspx 扩展名,它就无法工作吗? -
您是否也更改了
path=的web.config?尝试仅使用path="*"这应该通过您的处理程序发送任何请求。通常,您会尝试将请求限制为您希望必须处理的请求,即path="/payments。 -
您是否有理由从 Server.Transfer 切换到 RedirectPath?见stackoverflow.com/questions/1757336
-
我读过this article,它建议不要使用httphandler,并说要改用httpmodule。它似乎没有指定路径?此外,这也是 RedirectPath 代码的来源。
标签: asp.net vb.net url-rewriting