[英]Startup.cs migration from .net core 2.2 to .net core 3.1
I am migrating a .net core project to version 3.1, but I'm confused with this part of the startup.cs file:我正在将 .net 核心项目迁移到 3.1 版,但我对 startup.cs 文件的这一部分感到困惑:
services.AddControllers().AddNewtonsoftJson(opt =>
{
opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});
services.AddMvc(opt =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
.Build();
opt.Filters.Add(new AuthorizeFilter(policy));
opt.Filters.Add<ModelStateValidationFilter>();
//opt.Filters.Add<DbContextTransactionFilter>();
opt.Filters.Add<ExceptionsFilter>();
})
.AddFluentValidation(opt => opt.RegisterValidatorsFromAssemblyContaining<Business.Exceptions.NotFoundException>());
should I keep the addMvc()?我应该保留 addMvc() 吗?
With this, I am getting a 500 internal server error.有了这个,我得到一个 500 内部服务器错误。
AddControllers
or AddControllersWithViews
has replaced AddMvc
in .NET Core 3 AddControllers
或AddControllersWithViews
已替换AddMvc
Core 3 中的 AddMvc
Quite a lot has changed in v3 so it would be good to review the migration guides. v3 中发生了很多变化,因此最好查看迁移指南。
Migration guide for v2 to v3 v2 到 v3 的迁移指南
Docs example of changed Startup.cs in v3 v3 中更改 Startup.cs 的文档示例
If your error message is如果您的错误信息是
Handler was not found for request of type MediatR.IRequestHandler System.InvalidOperationException
You can use MediatR's MediatR.Extensions.Microsoft.DependencyInjection
package which includes a .AddMediatR()
extension method, allowing you to register all handlers and pre/post-processors in a given assembly.您可以使用 MediatR 的
MediatR.Extensions.Microsoft.DependencyInjection
package,其中包括一个.AddMediatR()
扩展方法,允许您在给定程序集中注册所有处理程序和前/后处理器。
It will search the assembly for any Handlers, Profiles etc and register them for use.它将在程序集中搜索任何处理程序、配置文件等并注册它们以供使用。
public void ConfigureServices(IServiceCollection services)
{
// ...
// Adds all MediatR handlers from the Assembly containing Startup
services.AddMediatR(typeof(Startup));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.