托管 .Net Core 和 Angular 应用程序的最佳方式?

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

我有我的 .Net Core API 和我的 Angular 站点,并且 运行 在本地。现在我想发布到 .Net 托管提供商,而不是 Azure。那么最好的方法是启用静态内容,然后构建我的 Angular 应用程序并将其放入 API 解决方案的 wwwroot 中吗?

旁注:如果重要的话,我使用的是 .net 核心 2.x。而且,Angular 我的意思是 Angular2 而不是 AngularJS。这是标准术语吗? :-)

要回答您的问题,是的,您应该启用静态内容并将您的 Angular 应用程序和文件构建到 wwwroot

这是最简单的 Startup 您可以用来在 .NET Core 2.0 上提供 Angular 应用程序。

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

如您所见,不需要 MVC 或 razor 视图。