Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC to ignore ".html" at the end of all url

I am new to asp.net mvc and now struggling with url routing. I'm using asp.net mvc 3 RC2.

How can I create a url routing that IGNORES the very end extension in url. the extension can be: .html, .aspx, .php, .anything.

For example, these urls:

/Home.html
/Home.en
/Home.fr
/Home

should go to Home controller?

one more example:

/Home/About.html
/Home/About.en
/Home/About.fr
/Home/About

should go to Home controller and About action.

thank you :)

like image 791
Anwar Chandra Avatar asked Dec 15 '10 11:12

Anwar Chandra


1 Answers

I'm not sure if you're using IIS7, but if so, then I would recommend a rewrite rule which checks for urls ending in .xyz and then doing a rewrites for them without the .xyz.

Something like this:

<rewrite>
  <rules>
    <rule name="HtmlRewrite">
      <match url="(.*)(\.\w+)$" />
      <action type="Rewrite" url="{R:1}" />
    </rule>
  </rules>
</rewrite>

This will handle the use cases you suggested. Anything that ends with an extension and some characters will be rewritten to a url without the extension. The benefit of this is that you will only need one route because everything goes into your application without one.

like image 108
Dan Atkinson Avatar answered Sep 18 '22 19:09

Dan Atkinson