在本地我可以刷新一个页面,它将加载(即,localhost:4200/customers
),但在生产-托管在myasp.net IIS网站-当我去www.mysite.com/customers
我得到一个404,但如果我去:
www.mysite.com
- 点击"客户"按钮
- 我被重定向到
www.mysite.com/customers
,页面显示正确
然而,如果我刷新,我得到一个404!
我做了一些调查,看看我可以:
- 更新web。配置与
<rewrite>
<rules>
<rule name="Angular 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>
但是没有web.config
HashLocationStrategy
,
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HashLocationStrategy, LocationStrategy } from '@angular/common';
const routes: Routes = [
// Define your application routes here
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [{provide: LocationStrategy, useClass: HashLocationStrategy}]
})
export class AppRoutingModule { }
但是我从ChatGPT得到这个,所以我不太确定。想看看你说…
刚刚添加了web。在根配置,现在它工作了!!
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Angular 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>
最后ChatGPT给了我写技术的答案!呜呼!