Priyanka
Priyanka

Reputation: 300

Removing .aspx extension from URL using URL Rewriting

I am implementing URL rewriting in my project. I added the rules for rewriting from IIS using URL Rewrite. Below is the code of my web.config file in which the rule is added:

<system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <rewrite>
            <rules>
                <rule name="URLRedirect" stopProcessing="true">
                    <match url="^([a-z0-9/]+).aspx$" />
                    <action type="Redirect" url="{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>

But the problem is i had written the rule for removing just the extension i.e .aspx and i want my URL to look like

http://localhost:58370/URLRedirect/Default.

But now it is displaying it as http://localhost:58370/URLRedirect/ How can this issue be solved.....

Upvotes: 2

Views: 7440

Answers (2)

Michael
Michael

Reputation: 11

You can do it this way.

Install-Package Microsoft.AspNet.FriendlyUrls

Then Add Global.asax file in your application

void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}

Test your application, you would see that the .aspx extension has been removed.

Upvotes: 1

Priyanka
Priyanka

Reputation: 300

Finally i was able to solve my problem of removing the .aspx extensions from my files. I wanted my URL to look like: http://localhost:58370/ShowPage instead of http://localhost:58370/ShowPage.aspx

1)I added ShowPage.aspx page inside a folder named ShowPage. 2)And following are the rules that i added to my web.config file:

 <rewrite>
          <rules>
            <clear />
            <rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
              <match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
              <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
              <action type="Redirect" url="{R:1}" />
            </rule>
            <rule name="RewriteASPX" enabled="true">
              <match url="(.*)" />
              <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
              </conditions>
              <action type="Rewrite" url="{R:1}.aspx" />
            </rule>
          </rules>
        </rewrite>

Upvotes: 7

Related Questions