How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode?

后端 未结 7 2091
春和景丽
春和景丽 2020-11-22 12:12

I have the AngularJS seed project and I\'ve added

$locationProvider.html5Mode(true).hashPrefix(\'!\');

to the app.js file. I want to confi

相关标签:
7条回答
  • 2020-11-22 12:40

    The issue with only having these two conditions:

      <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
      <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    

    is that they work only as long as the {REQUEST_FILENAME} exists physically on disk. This means that there can be scenarios where a request for an incorrectly named partial view would return the root page instead of a 404 which would cause angular to be loaded twice (and in certain scenarios it can cause a nasty infinite loop).

    Thus, some safe "fallback" rules would be recommended to avoid these hard to troubleshoot issues:

      <add input="{REQUEST_FILENAME}" pattern="(.*?)\.html$" negate="true" />
      <add input="{REQUEST_FILENAME}" pattern="(.*?)\.js$" negate="true" />
      <add input="{REQUEST_FILENAME}" pattern="(.*?)\.css$" negate="true" />
    

    or a condition that matches any file ending:

    <conditions>
      <!-- ... -->
      <add input="{REQUEST_FILENAME}" pattern=".*\.[\d\w]+$" negate="true" />
    </conditions>
    
    0 讨论(0)
提交回复
热议问题