Wompguinea
Wompguinea

Reputation: 378

Make a single page with friendly URL?

I am working on a new page for a website at work, my boss would like it to have a friendly URL.

Currently website.com/account-payments.aspx and he wants it to be website.com/payments/

Problem is, I can't figure out how to rewrite the url properly. I've seen a NuGet package recommended "Microsoft ASP.NET Friendly URLs" but I don't have admin rights on this network and I can't install anything new. My boss is less than helpful regarding this.

Are there any built in features of ASP.net that will allow me to rewrite the url?

Edit

I have added this class to my App_Code folder.

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

With the following handler in my web.config file:

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

And now, instead of a generic 404 error, I get the 500 internal server error when attempting to browse to website.com/payments/

Edit 2: Electric Boogaloo

So after Googlin about I found someone having a similar problem and they fixed theirs by adding an extra handler to the web.config.

<httpHandlers> <add verb="*" path="*/account-payments.aspx" type="PaymentsHandler" validate="false"/> </httphandlers>

I checked my webconfig and sure enough there are existing handlers in both <handlers> and <httpHandlers>

Now, instead of the old 500 error, I'm getting a 403 error. Getting real confused.

Edit 3: The Editing

Progress! I switched from HTTPHandler to HTTPModule and rigged up my PaymentsHandler.vb to match:

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

Now I can see that the rewrite is happening! Yay! So when I browse to website/account-payments.aspx it automagics to website/payments/ and then I get a 403 - Forbidden error.

So it's rewriting, and redirecting... but apparently the page is now forbidden?

Upvotes: 1

Views: 407

Answers (2)

Dave Anderson
Dave Anderson

Reputation: 12294

You can write your own HttpHandler that you can wire up in the web.config. This class can check the HttpRequest url and map that to your aspx page.

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

Obviously the logic to map the requests needs to be sufficiently robust. You add it to the web.config like this;

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

See;

Upvotes: 1

Rachel Gallen
Rachel Gallen

Reputation: 28573

You could try this

   void Application_BeginRequest(object sender, EventArgs e) {

    string fullOrigionalpath = Request.Url.ToString();

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

I recommend you read this blog for other methods

Upvotes: 1

Related Questions