5

I've created an ASP.NET Core project which serves an Angular app using services.AddSpaStaticFiles(), app.UseSpaStaticFiles(), app.UseSpa(), etc.

But it serves the application directly from http://localhost:1234, while I want to serve it from http://localhost:1234/angular/MyAngularApp1/, while I want to use http://localhost:1234/ for an API controller.

How can I achieve that?

Note: I need it to work in a production build. Some solutions appear to work, but in reality they work only when you run the site from Visual Studio, and stop working once you publish/deploy to IIS or Kestrel.

2
  • can you explain the usecase for this approach? we have the api on one port and the angular app on the other (with cors policy on the api of course), on production the api runs in a subdomain Commented Nov 21, 2019 at 12:08
  • @DaniëlTulp, Sure - our production build has to accessed from within a network that is firewalled. Clients in the network can't access our app directly, but there is a reverse-proxy that was setup using IIS and URLRewrite to rewrite requests that start with a particular prefix. Any request that doesn't start will be dropped by the proxy. We don't have access to reconfigure that proxy so.... Commented Nov 22, 2019 at 13:00

4 Answers 4

4

Eventually I found a function that pretty much achieves this:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UsePathBase("/MyPrefix/");

    if (env.IsDevelopment())
    .......

See https://www.billbogaiv.com/posts/net-core-hosted-on-subdirectories-in-nginx for more info.

Sign up to request clarification or add additional context in comments.

Comments

2

You can define a base url for your application like this

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

Comments

2

If you want to specify base URL for view from 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="/ClientApp">

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

Comments

0

You can also use

         app.Map(new PathString("/MyPrefix"), appMember =>
         {
             appMember.UseSpa(spa =>
             {
                 spa.Options.SourcePath = "ClientApp";

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

and for API

app.Map("/api", api => 
    {
        api.UseIdentityServerAuthentication(new IdentityServerAuthenticationOptions
        {
            Authority = "https://localhost:5000/openid"
        });

        api.UseMvc();
    });

So you can run multiple parallel pipeline in same application. Read more about it here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.