Angular 6 + ASP Service Fabric 中的 .NET Core MVC
Angular 6 + ASP .NET Core MVC inside Service Fabric
我正在尝试 运行 'Stateless ASP.NET Core' API 项目,在 Service Fabric 中使用 Angular 6。
我做什么
- 创建 ASP.NET 核心 API 项目。
- 通过 运行ning 'ng new ClientApp 在 ASP.NET 核心项目中创建一个 Angular 项目。
- 运行 在 Angular 项目中构建以生成文件。
编辑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 核心 API 这种方法有效。 !!
如果我在 Service Fabric 中创建相同的内容,我会收到以下错误:
//编辑
解决了
我能够按照此 link https://blog.bridging-it.de/blog/blogeintrag_4480.html
中的指南进行操作
它是 德语 但你应该能够在 Google 翻译的帮助下弄明白:)
将此 NuGet 包添加到您的项目中:
NWebsec.AspNetCore.Middleware
并配置如下:
app.UseHsts(h=> h.MaxAge(days:100).preload())
我正在尝试 运行 'Stateless ASP.NET Core' API 项目,在 Service Fabric 中使用 Angular 6。
我做什么
- 创建 ASP.NET 核心 API 项目。
- 通过 运行ning 'ng new ClientApp 在 ASP.NET 核心项目中创建一个 Angular 项目。
- 运行 在 Angular 项目中构建以生成文件。
编辑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 核心 API 这种方法有效。 !!
如果我在 Service Fabric 中创建相同的内容,我会收到以下错误:
//编辑
解决了我能够按照此 link https://blog.bridging-it.de/blog/blogeintrag_4480.html
中的指南进行操作它是 德语 但你应该能够在 Google 翻译的帮助下弄明白:)
将此 NuGet 包添加到您的项目中:
NWebsec.AspNetCore.Middleware
并配置如下:
app.UseHsts(h=> h.MaxAge(days:100).preload())