Waste/Waste.Web.Core/Startup.cs

97 lines
3.5 KiB
C#

using Furion;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Nirvana.Common;
using Serilog;
using System.IO;
using System.Threading.Tasks;
using Waste.Core;
namespace Waste.Web.Core
{
public class Startup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSession();//使用Session
//services.AddJwt<JwtHandler>(options =>
//{
// options.DefaultScheme = null;
//});
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
services.AddCorsAccessor();
// services.AddRemoteRequest();
services.AddHttpClient();
services.AddControllers(options => {
options.EnableEndpointRouting = true;
})
.AddInjectWithUnifyResult<RESTfulResultProvider>();
services.AddRazorPages(options =>
{
options.Conventions.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute());
}).AddNewtonsoftJson(options =>
{
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; //日期格式化
options.SerializerSettings.ContractResolver = new CustomContractResolver();
});
services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();
services.AddTaskScheduler();
#region IP
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
MyHttpContext.serviceCollection = services;
#endregion
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSession();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// 添加规范化结果状态码,需要在这里注册
app.UseUnifyResultStatusCodes();
// 或者更多配置
// app.UseUnifyResultStatusCodes(options => { options.Return200StatusCodes = new [] { 401, 403 }; });
//允许body重用
app.Use(next => context =>
{
context.Request.EnableBuffering();
return next(context);
});
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSerilogRequestLogging(opts => {
opts.EnrichDiagnosticContext = LoggerHelper.EnrichFromRequest;
}); // 必须在 UseStaticFiles 和 UseRouting 之间,记录请求日志
app.UseRouting();
app.UseCorsAccessor();
// app.UseAuthentication();
app.UseAuthorization();
app.UseInject();
app.UseEndpoints(endpoints =>
{
//endpoints.MapGet("/index.html", async context => {
// await context.Response.WriteAsync("Hello");
//});
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
}
}