Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to host a .Net Core and Angular app?

I have my .Net Core API and my Angular site all built and running locally. Now I want to publish to an .Net Hosting provier, not Azure. So would the best way be to enable static content and then build my Angular app and drop it in the wwwroot of the API solution?

side notes: Im using .net core 2.x, if that matters. And, by Angular I mean Angular2 not AngularJS. Is this standard terminology yet? :-)

like image 250
wondernate Avatar asked Feb 06 '18 23:02

wondernate


Video Answer


1 Answers

To answer your question, yes, you should enable static content and build your Angular app and files out to wwwroot.

This is the simplest Startup you can use to serve an Angular app on .NET Core 2.0.

public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // this will serve wwwroot/index.html when path is '/'
        app.UseDefaultFiles();

        // this will serve js, css, images etc.
        app.UseStaticFiles();

        // this ensures index.html is served for any requests with a path
        // and prevents a 404 when the user refreshes the browser
        app.Use(async (context, next) =>
        {
            if (context.Request.Path.HasValue && context.Request.Path.Value != "/")
            {
                context.Response.ContentType = "text/html";

                await context.Response.SendFileAsync(
                    env.ContentRootFileProvider.GetFileInfo("wwwroot/index.html")
                );

                return;
            }

            await next();
        });
    }
}

As you can see there's no need for MVC or razor views.

like image 199
Brad Avatar answered Sep 19 '22 12:09

Brad