AngularJs Routing, MVC6 / vNext / .NET5 and MVC View

All examples of using AngularJs with MVC6 / vNext / .NET5 use the static index.html file, which is located inside wwwroot. Did this, but now I need to load the MVC index.cshtml page so that I can add the parameters that are in my config.json to the page.

All I want is the default (Home) page, which loads no matter what URL is requested, provided that the URL is not a js / css file or api controller.

I tried adding this to my method Configure(IApplicationBuilder app, IServiceProvider serviceProvider), but now my js and css files completely canceled the Home / Index.cshtml file

app.UseMvc(options =>
{
    options.MapRoute("Api",
        template: "api/{version}/{controller}/{action?}",
        defaults: new { version = "v1", controller = "Page" });

    options.MapRoute(
        name: "default",
        template: "{*url}",
        defaults: new { controller = "Home", action = "Index" });
});

http://localhost:port, . angularJs URL-,

http://localhost:6000/page
http://localhost:6000/page/9
http://localhost:6000/another
http://localhost:6000/another/9/sub

Home/Index.cshtml angular .

- web.config, IIS,

, js/css, . angular, .

app.UseMvc(options =>
{
    options.MapRoute("Api",
        template: "api/{version}/{controller}/{action?}",
        defaults: new { version = "v1", controller = "Page" });

    options.MapRoute(
        name: "default",
        template: "{controller}/{action?}",
        defaults: new { controller = "Home", action = "Index" });
});

2

public void Configure(IApplicationBuilder app, IServiceProvider serviceProvider)
{
    // add automapper registrations
    app.RegisterMappingFiles();

    // Enable all static file middleware
    app.UseStaticFiles();

    // Enable Mvc for controllers
    app.UseMvc(options =>
    {
        options.MapRoute(
            name: "default",
            template: "{controller}/{action?}",
            defaults: new { controller = "Home", action = "Index" });
    });

}
+4
1

, , , MVC:

    public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
    {
        app.UseStaticFiles();

        app.UseMvc(options =>
        {
            options.MapRoute("Api",
        template: "api/{version}/{controller}/{action?}",
        defaults: new { version = "v1", controller = "Page" });

            options.MapRoute(
                name: "default",
                template: "{*url}",
                defaults: new { controller = "Home", action = "Index" });
        });

    }

invoke app.UseStaticFiles() app.UseMvc(), , , , .

Documentaion docs.asp.net

+3

Source: https://habr.com/ru/post/1611731/


All Articles