'无法从程序集'Microsoft.AspNetCore.Mvc.Formatters.Json 加载类

4

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

问题描述

限时送ChatGPT账号..

我在我的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14