Ai
1 Star 0 Fork 0

周鹏/ASP.NET-Core-3.x-REST-API-Tutorial-Code

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Startup.cs 4.26 KB
一键复制 编辑 原始数据 按行查看 历史
Dave Young 提交于 2020-02-22 14:04 +08:00 . finish
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();
});
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zphcxj/ASP.NET-Core-3.x-REST-API-Tutorial-Code.git
git@gitee.com:zphcxj/ASP.NET-Core-3.x-REST-API-Tutorial-Code.git
zphcxj
ASP.NET-Core-3.x-REST-API-Tutorial-Code
ASP.NET-Core-3.x-REST-API-Tutorial-Code
master

搜索帮助