Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL rewrite in ASP.NET 5

I'm using ASP.NET 5 where the entire folder structure is changed and the web.config is replaced (compared to previous ASP.NET versions). I'm doing client side routing using angularJS and I have this route:

.when('/movies/add', {
            templateUrl: '/Views/add.html',
            controller: 'MoviesAddController'
        })

Everything works as longs as I start on my index.html and click on a link to /movies/add. If I reload the page using the /movies/add URL, the server gives me a 404. According to this tutorial I should do a rewrite in web.config, like this:

<!-- from http://stackoverflow.com/questions/25916851/wrapping-staticfilemiddleware-to-redirect-404-errors -->

<configuration>
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
  <rewrite>
    <rules>
      <!--Redirect selected traffic to index -->
      <rule name="Index Rule" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_URI}" matchType="Pattern" pattern="^/api/" negate="true" />
        </conditions>
        <action type="Rewrite" url="/index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>
</configuration>  

I am using IIS Express 10.0 (in Windows 10 preview). I understand that the part in web.config should still exist in ASP.NET 5 to configure IIS but I don't get any result from this. Do I need to do something different using IIS Express? Is there another, more general, solution provided in ASP.NET 5?

Thanks!

like image 845
mikeesouth Avatar asked Jun 19 '15 18:06

mikeesouth


People also ask

What is URL Rewrite in asp net?

The concept of URL rewriting is simple. When a client sends a request to the Web server for a particular URL, the URL rewriting module analyzes the requested URL and changes it to a different URL on the same server.


1 Answers

web.config is still supported but it should go into wwwroot folder. You may be missing Url Rewrite module for IIS.

Alternatively, you can write a custom OWIN middleware to support html5 routing mode.

See this for an example: http://geekswithblogs.net/shaunxu/archive/2014/06/10/host-angularjs-html5mode-in-asp.net-vnext.aspx

like image 93
gaiazov Avatar answered Oct 31 '22 18:10

gaiazov