我在asp.net core 2.1中有一个后端,在8月6日中有一个前端,我将后端放入ClientApp文件夹中,我已经添加了packMicrosoft.AspNetCore.SpaServices和Microsoft.AspNetCore.SpaServices.Extensions.I一切都像(http://fiyazhasan.me/migrating-from-the-old-asp-net-core-angular-spa-template-to-the-newer-one/),但我想启动我的应用时遇到错误。
namespace XonadonUz
{
public class Startup
{
private readonly IConfigurationRoot _config;
public IContainer ApplicationContainer { get; private set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.SetBasePath(env.ContentRootPath);
_config = builder.Build(); ;
}
public IConfiguration Configuration { get; }
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddCustomizedMvc();
services.AddCustomAutoMapper();
services.AddDbContext();
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
services.AddCors(option =>
option.AddPolicy("AllowAll", p =>
p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
)
);
services.AddCustomAuthentication();
services.AddCustomAuthorization();
services.AddCustomIdentity();
services.Configure<FormOptions>(x =>
{
// set MaxRequestBodySize property to 200 MB
x.MultipartBodyLengthLimit = 209715200;
});
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("doc", new Info { Title = "XonadonUz API" });
c.OperationFilter<FileOperationFilter>();
});
return services.ConfigureAutofac(ApplicationContainer);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
EmailTemplates.Initialize(env);
Core.ServiceProvider.Services = app.ApplicationServices;
#region Configure swagger endpoints
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.RoutePrefix = AppSettings.Instance.SwaggerRoutePrefix;
c.SwaggerEndpoint("/swagger/doc/swagger.json", "doc");
});
#endregion
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
app.UseSpaStaticFiles();
app.UseExceptionHandler(builder =>
{
builder.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
context.Response.AddApplicationError(error.Error.Message);
await context.Response.WriteAsync(error.Error.Message).ConfigureAwait(false);
}
});
});
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "Storage")),
RequestPath = new PathString("/Storage")
});
app.UseAuthentication();
app.UseCors("AllowAll");
app.UseMvc();
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");
spa.Options.StartupTimeout = TimeSpan.FromSeconds(120); // Increase the timeout if angular app is taking longer to startup
//spa.UseProxyToSpaDevelopmentServer("http://localhost:4200"); // Use this instead to use the angular cli server
}
});
}
}
}
namespace XonadonUz
{
public class Program
{
public static void Main(string[] args)
{
var host = BuildWebHost(args);
#region Seed Data
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var databaseInitializer = services.GetRequiredService<IDbInitializer>();
databaseInitializer.SeedAsync().Wait();
}
catch (Exception ex)
{
var logger = services.GetRequiredService<ILogger<Program>>();
logger.LogCritical("Error whilst creating and seeding database", ex);
}
}
#endregion
CultureHelper.SetDefaultCulture();
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls(new string[] { AppSettings.Instance.Hosts })
.UseStartup<Startup>()
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = 209715200;
})
.UseContentRoot(Directory.GetCurrentDirectory())
.Build();
}
}