Getting 404 error on page refresh in Angular 4

邮差的信 提交于 2019-12-22 09:36:47

问题


We have deployed angular 4 application using 'ng build --prod' cli command and created 'dist' folder hosted on IIS 8.5, when we run the application then it's working fine but if pressed F5 key then getting 404 error.

Note: please don't suggest to use (useHash:true) if suggested then how we can remove '#' from url.

Appreciated help if some one can suggest us.


回答1:


You can write change your web.config file like below

<configuration>
    <system.webServer>
         <rewrite>
            <rules>
              <rule name="AngularJS Routes" stopProcessing="true">
                <match url=".*" />
                <conditions logicalGrouping="MatchAll">
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="/" />
              </rule>
            </rules>
          </rewrite>
          <caching enabled="true" enableKernelCache="true">
              <profiles>
                  <add extension=".js" policy="DisableCache" kernelCachePolicy="DisableCache" />
              </profiles>
          </caching>
    </system.webServer>
</configuration>

let's i explain more this configuration

<match url=".*" />

this match all url which is coming and than it's redirect in your base path using action

<action type="Rewrite" url="/" />

If you have set your project in subfolder than change your url="/subfolder/".



来源:https://stackoverflow.com/questions/47196132/getting-404-error-on-page-refresh-in-angular-4

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