'无法从程序集'Microsoft.AspNetCore.Mvc.Formatters.Json,版本= 3.0.0.0中加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions' [英] 'Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0

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

问题描述

我正在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类,

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 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,版本= 3.0.0.0中加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆