Waste/Waste.Web.Core/Startup.cs

94 lines
3.4 KiB
C#
Raw Normal View History

2021-04-30 14:52:42 +08:00
using Furion;
using Microsoft.AspNetCore.Builder;
2021-05-27 16:58:40 +08:00
using Microsoft.AspNetCore.DataProtection;
2021-04-30 14:52:42 +08:00
using Microsoft.AspNetCore.Hosting;
2021-05-27 16:58:40 +08:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
2021-04-30 14:52:42 +08:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2021-05-27 16:58:40 +08:00
using Nirvana.Common;
2021-04-30 14:52:42 +08:00
using Serilog;
2021-05-27 16:58:40 +08:00
using System.IO;
using System.Threading.Tasks;
2021-04-30 14:52:42 +08:00
namespace Waste.Web.Core
{
public class Startup : AppStartup
{
public void ConfigureServices(IServiceCollection services)
{
2021-05-27 16:58:40 +08:00
services.AddSession();//使用Session
//services.AddJwt<JwtHandler>(options =>
//{
// options.DefaultScheme = null;
//});
services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
2021-04-30 14:52:42 +08:00
services.AddCorsAccessor();
// services.AddRemoteRequest();
2021-05-27 16:58:40 +08:00
services.AddHttpClient();
services.AddControllers(options => {
options.EnableEndpointRouting = true;
})
2021-04-30 14:52:42 +08:00
.AddInjectWithUnifyResult<RESTfulResultProvider>();
2021-05-27 16:58:40 +08:00
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();
2021-05-27 16:58:40 +08:00
#region IP
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
MyHttpContext.serviceCollection = services;
#endregion
2021-04-30 14:52:42 +08:00
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
2021-05-27 16:58:40 +08:00
app.UseSession();
2021-04-30 14:52:42 +08:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
2021-05-27 16:58:40 +08:00
// 添加规范化结果状态码,需要在这里注册
app.UseUnifyResultStatusCodes();
// 或者更多配置
// app.UseUnifyResultStatusCodes(options => { options.Return200StatusCodes = new [] { 401, 403 }; });
//允许body重用
app.Use(next => context =>
{
context.Request.EnableBuffering();
return next(context);
});
2021-04-30 14:52:42 +08:00
app.UseHttpsRedirection();
2021-05-27 16:58:40 +08:00
app.UseStaticFiles();
2021-04-30 14:52:42 +08:00
app.UseSerilogRequestLogging(); //记录请求日志,必须在 UseStaticFiles 和 UseRouting 之间
app.UseRouting();
app.UseCorsAccessor();
2021-05-27 16:58:40 +08:00
// app.UseAuthentication();
2021-04-30 14:52:42 +08:00
app.UseAuthorization();
2021-05-27 16:58:40 +08:00
app.UseInject();
2021-04-30 14:52:42 +08:00
app.UseEndpoints(endpoints =>
{
2021-05-27 16:58:40 +08:00
//endpoints.MapGet("/index.html", async context => {
// await context.Response.WriteAsync("Hello");
//});
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
2021-04-30 14:52:42 +08:00
});
}
}
}