使用asp.net核心启用角度路由

问题描述 投票:0回答:1

我正在尝试构建一个带有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解决。有任何想法吗 ?

c# asp.net angularjs asp.net-core
1个回答
1
投票

答案(主要是)在UseMVC选项中...您应该实现服务器端路由,如图所示

    app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

    routes.MapSpaFallbackRoute("spa-fallback", new { controller = "Home", action = "Index" });
});

看看这个link

© www.soinside.com 2019 - 2024. All rights reserved.