【问题标题】:Best way to host a .Net Core and Angular app?托管 .Net Core 和 Angular 应用程序的最佳方式?
【发布时间】:2018-03-19 08:33:14
【问题描述】:

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

旁注:如果这很重要,我正在使用 .net core 2.x。而且,我所说的 Angular 是指 Angular2 而不是 AngularJS。这是标准术语吗? :-)

【问题讨论】:

    标签: .net angular .net-core-2.0


    【解决方案1】:

    要回答您的问题,是的,您应该启用静态内容并将您的 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 视图。

    【讨论】:

    • 高五!
    • 呵呵...我向上箭头而不是检查它。 :-) 谢谢提醒。
    猜你喜欢
    • 2021-10-30
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    • 2017-07-26
    • 2021-11-04
    • 1970-01-01
    相关资源
    最近更新 更多