Dot-Net

託管 .Net Core 和 Angular 應用程序的最佳方式?

  • March 19, 2018

我的 .Net Core API 和 Angular 站點都在本地建構和執行。現在我想發佈到 .Net 託管提供商,而不是 Azure。那麼最好的方法是啟用靜態內容,然後建構我的 Angular 應用程序並將其放入 API 解決方案的 wwwroot 中嗎?

旁注:如果這很重要,我正在使用.net core 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 視圖。

引用自:https://stackoverflow.com/questions/48653483