Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rewrite URL in an ASP.net site

I want to rewrite URL in an asp.net site

What i need is i don't want the user to see in which language the site was created

i.e it should not have www.examplesite.com/index.aspx as address

instead i want it as www.examplesite.com/index

I don't want the user to see the extension of files

If this question is not related to Stackoverflow please redirect this question to the respective site of Stack Exchange.

Any help is appreciated.

like image 535
Amarnath Balasubramanian Avatar asked Mar 31 '14 07:03

Amarnath Balasubramanian


1 Answers

You can do this at a simple level in the Global.asax file like this:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires at the beginning of each request

    Dim path As String = HttpContext.Current.Request.Path

    If path.ToLower.EndsWith(".aspx") Then
        path = path.Substring(0, path.Length - 5)
        Response.Redirect(path, True)
    Else
        path += ".aspx"
        Context.RewritePath(path)
    End If

End Sub

If you have other files that are requested such as .png files, you may need some additional logic to filter these out.

like image 76
Matt Wilko Avatar answered Oct 19 '22 00:10

Matt Wilko