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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

The issue related with your question is the URL Rewriting in ASP.Net. For URL rewriting there are various approaches: writing rewrite module in IIS or using ASP.NET Web.config file.

For using web.config file, you’ve to first of all add rewrite configuration section and the define the rewrite rule as per your requirement within the <rewrite > </rewrite> tags.

For more details: follow this Link.

I hope this will help you.

Method 3

In web.config file, add below code.

<rewrite>
       <rules>
            <clear />

                <rule name="exampleredirect" stopProcessing="true">
                    <match url="^index.aspx" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Redirect" url="/index" />
                </rule>
        </rules>
</rewrite>

Please refer the below link.

http://forums.asp.net/t/1910607.aspx?web+config+rewrite+rule

Method 4

There is a nuget package for this I think, its called Friendly Urls

Method 5

In the web.config you’d have to add a section called <rewrite> </rewrite>, then you would add rules/rule names as I’ve done below:

            <rewrite><rules><clear />
               <rule name="redirectrule" stopProcessing="true">
               <match url="^index.aspx" />
               <conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
               <action type="Redirect" url="/index" />
            </rule></rules></rewrite>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x