3 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個(gè)贊
這里的問題是你有一個(gè) SPA,而 Angular 有它自己的路由,但是當(dāng)你嘗試使用像https://localhost:5001/login這樣的 URL 時(shí),這條路由是由 MVC 而不是由 Angular 提供的,MVC 會(huì)找不到合適的控制器,結(jié)果返回 404。
你應(yīng)該做的是返回 index.html 以響應(yīng)此類請求,以便 Angular 路由可以處理它。你可以看看這篇文章:
https://www.toptal.com/angular/angular-5-asp-net-core

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
原來我的問題出在我處理 404 的方式上。我context.Request.Path = "/index.html";在 Startup.cs中做的,但它仍然無法解析 html 文件,所以我讓它重定向回根路徑:
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 &&
!Path.HasExtension(context.Request.Path.Value))
{
// This is what I changed. I need to resolve to "/" instead of "/index.html"
context.Request.Path = "/";
context.Response.StatusCode = 200;
await next();
}
});

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用此代碼:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
}
- 3 回答
- 0 關(guān)注
- 213 瀏覽
添加回答
舉報(bào)