'无法从程序集'Microsoft.AspNetCore.Mvc.Formatters.Json 加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions',版本= 3.0.0.0

#39;Could not load type #39;Microsoft.AspNetCore.Mvc.MvcJsonOptions#39; from assembly #39;Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0(无法从程序集Microsoft.AspNetCore.Mvc.Formatters.Json 加载类型Microsoft.AspNetCore.Mvc.MvcJsonOptions,版本= 3.0.0.0)

本文介绍了'无法从程序集'Microsoft.AspNetCore.Mvc.Formatters.Json 加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions',版本= 3.0.0.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 netcoreapp3.0 Web 应用程序中使用 netstandard2.1 库.在 Startup 中添加我的服务时,我收到以下错误:

I'm using netstandard2.1 library in my netcoreapp3.0 web application. When adding my service in Startup, I'm getting the below error:

'无法加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions'程序集 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0

'Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0

我还在我的类库中使用 Microsoft.AspNetCore.Mvc 2.2.0 包中的一些功能.

I'm also using some features from Microsoft.AspNetCore.Mvc 2.2.0 package in my class library.

这是我的库 .csproj,

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
  </ItemGroup>

</Project>

这是我的库中的 ServiceExtensions 类,

Here is my ServiceExtensions class from my library,

public static class ServiceExtensions
{
    public static IMvcBuilder AddMyLibrary(this IMvcBuilder builder)
    {
        builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        builder.AddJsonOptions(options =>
        {
            options.SerializerSettings.ContractResolver = new DefaultContractResolver();
        });
        builder.Services.ConfigureOptions<ConfigureLibraryOptions>();

        return builder;
    }
}

这是我的 ConfigureLibraryOptions 类,

public class ConfigureLibraryOptions : IConfigureOptions<MvcOptions>
{
    public void Configure(MvcOptions options)
    {
        options.ModelBinderProviders.Insert(0, new CustomBinderProvider());
    }
}

这是 Startup 中的 ConfigureServices

services.AddControllersWithViews().AddMyLibrary();

请帮助我为什么会收到此错误并帮助解决此问题?

Please help on why I'm getting this error and assist on how to solve this?

推荐答案

你得到这个错误的原因是 MvcJsonOptions 在 .NET Core 3.0 中被移除了;您可以在此处阅读更多有关重大更改的信息.

The reason why you're getting the error is because MvcJsonOptions was removed in .NET Core 3.0; you can read more about the breaking changes here.

这篇关于'无法从程序集'Microsoft.AspNetCore.Mvc.Formatters.Json 加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions',版本= 3.0.0.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:'无法从程序集'Microsoft.AspNetCore.Mvc.Formatters.Json 加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions',版本= 3.0.0.0