简体   繁体   中英

ASP.NET Core + Angular static route

I`ve created default ASP.NET Core + Angular project. I need to open angular app only by link /angular. Also when angular routing have to be under /angular link too, like "/angular/profile" - profile page, "/angular/item/22" - page with item. By links which don`t contains "/angular" I need open static files or MVC views.

Default project use SPA, so I dont want to change it.

app.UseSpa(spa =>
{
    spa.Options.SourcePath = "ClientApp";

    if (env.IsDevelopment())
    {
        spa.UseAngularCliServer(npmScript: "start");
    }
});

Is there any solution for it?

If you want to specify the base URL to view from the Angular ClientApp folder, try to set href attribute of base by going to ClientApp > src > index.html like below:

<head>
 <meta charset="utf-8">
 <title>ClientApp</title>
 <base href="/angular">

 <meta name="viewport" content="width=device-width, initial-scale=1">
 <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>

After that, the URL like this:

在此处输入图像描述

There is no need to change the UseSpa method, my UseSpa() method as below:

        app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501 
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
            }
        });

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