我正在尝试构建一个带有asp.net core
用于后端端的web应用程序和用于前端端的angular 1.5.x
。问题是如何使服务器分析角度路由。我将这些代码行添加到startup.cs
并且evry的工作正常:
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("Index.html");
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value))
{
context.Request.Path = "/Index.html";
await next();
}
})
.UseCors("AllowAll")
.UseDefaultFiles(options)
.UseStaticFiles()
.UseIdentity()
.UseMvc();
但我觉得这不是一个好习惯,这个问题应该在appsettings.json
解决。有任何想法吗 ?
答案(主要是)在UseMVC选项中...您应该实现服务器端路由,如图所示
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" });
});
看看这个link