代码拉取完成,页面将自动刷新
using System;
using System.Linq;
using AutoMapper;
using Marvin.Cache.Headers;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json.Serialization;
using Routine.Api.Data;
using Routine.Api.Services;
namespace Routine.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddHttpCacheHeaders(expires =>
{
expires.MaxAge = 60;
expires.CacheLocation = CacheLocation.Private;
}, validation =>
{
validation.MustRevalidate = true;
});
// services.AddResponseCaching();
services.AddControllers(setup =>
{
setup.ReturnHttpNotAcceptable = true;
setup.CacheProfiles.Add("120sCacheProfile", new CacheProfile
{
Duration = 120
});
}).AddNewtonsoftJson(setup =>
{
setup.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
}).AddXmlDataContractSerializerFormatters()
.ConfigureApiBehaviorOptions(setup =>
{
setup.InvalidModelStateResponseFactory = context =>
{
var problemDetails = new ValidationProblemDetails(context.ModelState)
{
Type = "http://www.baidu.com",
Title = "有错误!!!",
Status = StatusCodes.Status422UnprocessableEntity,
Detail = "请看详细信息",
Instance = context.HttpContext.Request.Path
};
problemDetails.Extensions.Add("traceId", context.HttpContext.TraceIdentifier);
return new UnprocessableEntityObjectResult(problemDetails)
{
ContentTypes = { "application/problem+json" }
};
};
});
services.Configure<MvcOptions>(config =>
{
var newtonSoftJsonOutputFormatter =
config.OutputFormatters.OfType<NewtonsoftJsonOutputFormatter>()?.FirstOrDefault();
newtonSoftJsonOutputFormatter?.SupportedMediaTypes.Add("application/vnd.company.hateoas+json");
});
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
services.AddScoped<ICompanyRepository, CompanyRepository>();
services.AddDbContext<RoutineDbContext>(option =>
{
option.UseSqlite("Data Source=routine.db");
});
services.AddTransient<IPropertyMappingService, PropertyMappingService>();
services.AddTransient<IPropertyCheckerService, PropertyCheckerService>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(appBuilder =>
{
appBuilder.Run(async context =>
{
context.Response.StatusCode = 500;
await context.Response.WriteAsync("Unexpected Error!");
});
});
}
// app.UseResponseCaching();
app.UseHttpCacheHeaders();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。