使用友好的网址创建一个页面?

时间:2015-09-01 03:36:04

标签: asp.net vb.net url-rewriting

我正在开发一个工作网站的新页面,我的老板希望它有一个友好的URL。

目前为website.com/account-payments.aspx,他希望它为website.com/payments/

问题是,我无法弄清楚如何正确地重写网址。我已经看过推荐的NuGet包" Microsoft ASP.NET Friendly URL"但我没有这个网络的管理员权限,我无法安装任何新的东西。我的老板对此不太有帮助。

是否有ASP.net的内置功能可以重写网址?

修改

我已将此类添加到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:电子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 - 禁止错误。

所以它重写并重定向......但显然该页面现在被禁止了?

2 个答案:

答案 0 :(得分:1)

您可以编写自己的HttpHandler,可以在web.config中连接。这个类可以检查HttpRequest url并将其映射到你的aspx页面。

<强> C#

public class PaymentsHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.RawUrl.EndsWith("/payments/"))
        {
            context.Server.TransferRequest("account-payments.aspx");
        }
    }
}

<强> VB

Public Class PaymentsHandler
    Implements IHttpHandler
    Public Sub ProcessRequest(context As HttpContext)
        If context.Request.RawUrl.EndsWith("/payments/") Then
            context.Server.TransferRequest("account-payments.aspx")
        End If
    End Sub
End Class

显然,映射请求的逻辑需要足够强大。您可以像这样添加web.config;

<system.webServer>
    <handlers>
      <add name="Payments" verb="*" path="/payments" type="PaymentsHandler" resourceType="Unspecified" />
    </handlers>
</system.webServer>

请参阅;

答案 1 :(得分:1)

你可以试试这个

   void Application_BeginRequest(object sender, EventArgs e) {

    string fullOrigionalpath = Request.Url.ToString();

    if (fullOrigionalpath.Contains("/account-payments.aspx")) {
        Context.RewritePath("/payments.aspx");
    }
} 

我建议你阅读this blog了解其他方法