angular当router使用userhash:false时路由404问题

安装iis urlrewrite2.0组件

在根目录下创建 Web.config

<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>
  </system.webServer>
</configuration>

.netcore 中间件方法

app.Use(async (context, next) =>  
{  
    await next();  
    if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))  
    {  
        context.Request.Path = "/index.html";  
        context.Response.StatusCode = 200;  
        await next();  
    }  
});  

纯angular项目可以移除其它的默认首页

DefaultFilesOptions options = new DefaultFilesOptions();  
options.DefaultFileNames.Clear();  
options.DefaultFileNames.Add("/index.html");  
app.UseDefaultFiles(options);  
app.UseStaticFiles(); 

相关文章:

  • 2022-01-01
  • 2022-12-23
  • 2021-07-01
  • 2022-01-02
  • 2021-07-30
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-19
  • 2022-01-17
  • 2021-07-26
  • 2021-08-05
  • 2021-12-24
  • 2021-07-22
相关资源
相似解决方案