简体   繁体   中英

Enable angular routes with asp.net core

i'm trying to build a web app with asp.net core for back-end side and angular 1.5.x for front-end side. the problem is haw to enable angular routes to be analyzed by the server. i added these lines of code to the startup.cs and evry thing works fine:

 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();

but i looks like it's not a good practice, and this problem should be solved in the appsettings.json . any ideas ?

The answer (mostly) is in the UseMVC options... you should implement server-side routes as shown

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

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

look at this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM