已部署到IIS 10的Angular应用刷新时404错误的解决方法
const routes: Routes = [//routes in here];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(routes, { useHash: true })
],
bootstrap: [AppComponent]
})
export class AppModule { }
#### index.html片段 ```html <html class="loading" lang="en" data-textdirection="ltr" data-critters-container> <head><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <base href="/"> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimal-ui"> <meta name="description" content="Modern admin is super flexible, powerful, clean & modern responsive Angular 15+ Bootstrap 5 Template with unlimited possibilities with bitcoin dashboard."> <meta name="keywords" content="admin template, modern admin template, dashboard template, flat admin template, responsive admin template, web app, crypto dashboard, bitcoin dashboard"> <meta name="author" content="EvolVision & CleVision">
解决方案
1. 修正web.config的重写路径
当前重写动作里的url="wwwroot/index.html"是错误的——IIS默认将wwwroot作为站点的静态文件根目录,所以重写应该直接指向根目录下的index.html:
修改重写规则中的action部分,取消注释正确的行并删除错误行:
<action type="Rewrite" url="/index.html" />
2. 确保Hash路由配置生效
你已经在Angular中配置了HashLocationStrategy和useHash: true,这种模式下路由会以/#/path形式存在,IIS不会把Hash后的内容当作实际文件路径解析,理论上不该出现刷新404。如果问题依旧,检查:
- 确认Angular发布包的路由配置未被覆盖
- 清除浏览器缓存后重新测试
- 若站点部署在IIS虚拟目录下,需同步修改
index.html中的<base href="/">为虚拟目录路径,比如<base href="/myapp/">
3. 检查IIS基础配置
- 确认已安装URL重写模块,没有这个模块的话,web.config中的重写规则无法生效
- 确保站点的应用程序池是集成模式,且.NET版本为4.0及以上,与web.config中
preCondition的配置匹配
内容的提问来源于stack exchange,提问作者Zidane
相关产品推荐
相关产品推荐

