URL rewrite in ASP.NET 5

一笑奈何 提交于 2019-12-10 13:35:33

问题


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!


回答1:


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



来源:https://stackoverflow.com/questions/30945402/url-rewrite-in-asp-net-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!