【问题标题】:Angular 6 + ASP .NET Core MVC inside Service FabricService Fabric 中的 Angular 6 + ASP .NET Core MVC
【发布时间】:2018-11-18 12:25:01
【问题描述】:

我正在尝试在 Service Fabric 中使用 Angular 6 运行“无状态 ASP.NET Core”API 项目。

我做什么

  • 创建 ASP.NET Core API 项目。
  • 通过运行 'ng new ClientApp 在 ASP.NET Core 项目中创建一个 Angular 项目。
  • 在 Angular 项目中运行 ng build 以生成文件。
  • 编辑 Startup.cs(见下面的代码)

    Startup.cs

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
    
            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                app.UseHsts();
            }
    
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSpaStaticFiles();
    
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action=Index}/{id?}");
            });
    
            app.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core,
                // see https://go.microsoft.com/fwlink/?linkid=864501
    
                spa.Options.SourcePath = "ClientApp";
    
                if (env.IsDevelopment())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });
        }
    }
    

结果

!!如果我创建一个普通的(非 Service Fabric)ASP.NET Core API,这种方法就有效。 !!

如果我在 Service Fabric 中创建相同的内容,我会收到以下错误:

//编辑

关注https://blog.bridging-it.de/blog/blogeintrag_4480.html解决了

【问题讨论】:

    标签: angular asp.net-core azure-service-fabric


    【解决方案1】:

    按照此链接https://blog.bridging-it.de/blog/blogeintrag_4480.html中的指南,我能够使其正常工作

    它是德语,但您应该能够在谷歌翻译的帮助下弄明白:)

    【讨论】:

      【解决方案2】:

      将此 NuGet 包添加到您的项目中:

      NWebsec.AspNetCore.Middleware

      并配置如下:

      app.UseHsts(h=> h.MaxAge(days:100).preload())

      【讨论】:

        猜你喜欢
        • 2016-09-18
        • 2019-07-10
        • 2017-06-18
        • 2020-09-22
        • 2022-08-14
        • 2019-04-07
        • 2017-06-08
        • 2019-04-17
        • 2019-06-21
        相关资源
        最近更新 更多