ASP.NET 5 中的 URL 重写

2024-04-03

我使用的是 ASP.NET 5,其中整个文件夹结构已更改,并且 web.config 已替换(与以前的 ASP.NET 版本相比)。我正在使用 angularJS 进行客户端路由,并且我有以下路线:

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

只要我开始使用 index.html 并单击指向 /movies/add 的链接,一切都会正常进行。如果我使用 /movies/add URL 重新加载页面,服务器会返回 404。根据本教程,我应该在 web.config 中进行重写,如下所示:

<!-- 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>  

我正在使用 IIS Express 10.0(在 Windows 10 预览版中)。我知道 web.config 中的部分应该仍然存在于 ASP.NET 5 中来配置 IIS,但我没有从中得到任何结果。 我需要使用 IIS Express 做一些不同的事情吗? ASP.NET 5 中是否提供了另一种更通用的解决方案?

Thanks!


web.config仍然受支持,但应该进入wwwroot文件夹。您可能缺少 IIS 的 URL 重写模块。

或者,您可以编写自定义 OWIN 中间件来支持 html5 路由模式。

请参阅此示例:http://geekswithblogs.net/shaunxu/archive/2014/06/10/host-angularjs-html5mode-in-asp.net-vnext.aspx http://geekswithblogs.net/shaunxu/archive/2014/06/10/host-angularjs-html5mode-in-asp.net-vnext.aspx

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ASP.NET 5 中的 URL 重写 的相关文章

随机推荐