【问题标题】:How to add Json Formatters to MvcCore?如何将 Json 格式化程序添加到 MvcCore?
【发布时间】:2019-05-29 16:54:23
【问题描述】:

我正在关注下一个 tutorial of IdentityServer4 implementation for API ,但我无法将方法 AddJsonFormatters() 调用为 services.AddMvcCore()。 我目前正在从 ASP.NET Core 3.0.0 中的空模板配置 API

我添加了 NuGet 包 Microsoft.AspNetCore.Mvc.Formatters.Json,但没有结果。 另外,我知道使用AddMvc() 而不是AddMvcCore() 将是部分解决方案,但我不能在AddMvc() 上使用AddAuthorization()

//code extracted from the link
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvcCore()
            .AddAuthorization()
            .AddJsonFormatters();
    }
}

这是我在上面看到的错误消息:

“IMvcCoreBuilder”不包含对 'AddJsonFormatters' 并且没有可访问的扩展方法 'AddJsonFormatters' 接受类型的第一个参数 可以找到“IMVCoreBuilder”(您是否使用了缺少的指令或 程序集参考?)

这是方法吗?我应该发送一个 MVCCoreBuilder 吗?我怎么做? MvcJsonMvcCoreBuilderExtensions.AddJsonFormatters Method

【问题讨论】:

  • 再次查看教程,已经更新到NetCore3。简而言之,您不再需要AddJsonFormatters(),因此只需将其删除即可。

标签: c# asp.net-core identityserver4


【解决方案1】:

当您拨打services.AddMvc() 时,您会收到一个IMvcBuilder

如果您想添加更多输出或输入格式化程序,IMvcBuilder 有一个扩展方法,您可以调用 AddMvcOptions 波纹管您有一个添加的 XmlDataContractSerializerOutputFormatter 示例

    mvcBuilder.AddMvcOptions(options =>
        {
            options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
            options.InputFormatters.Add(new XmlDataContractSerializerInputFormatter(options));

Mvc 已经有一个 JsonOutputFormatter ,所以在 AddMvcOptions 里面你可以得到它,如果你需要的话,还可以添加你自己的自定义媒体类型。

var jsonOutputFormatter = options.OutputFormatters.OfType<JsonOutputFormatter>().FirstOrDefault();

        if (jsonOutputFormatter != null)
        {
            jsonOutputFormatter.SupportedMediaTypes.Add(HttpMediaTypes.Vnd+json.all);
            jsonOutputFormatter.SupportedMediaTypes.Add(HttpMediaTypes.ApplicationOctetStream);
        }

【讨论】:

    【解决方案2】:

    据我了解,.NET Core 3.0 中还没有 MvcJsonMvcCoreBuilderExtensions 类。

    最终我在创建 Api 项目时添加了-f 参数:

    dotnet new web -n Api -f netcoreapp2.2
    

    而不是

    dotnet new web -n Api
    

    它为 .NET Core 2.2 制作了 Api 项目,因此您可以阅读教程。

    【讨论】:

    • 是的,我从 pc 中删除了高于 2.2 的 sdk,因此它将使用 2.2 构建一个新的 dotnet new web 项目
    【解决方案3】:

    我遇到了同样的问题。我发现我在使用当前版本的 dotnet core 实现它时使用了错误的教程。 -AnorZaken 的评论有帮助:

    再次查看教程,已经更新到NetCore3

    查看教程页面侧边栏的顶部。如果在 IdentityServer4 标题下显示“Release”,那就不行了。

    侧边栏底部有一个下拉菜单,您可以在其中选择“3.1”。

    【讨论】:

      【解决方案4】:

      使用这个:

      If MVC
      
      public void ConfigureServices(IServiceCollection services)
              {
                  services.AddControllersWithViews()
          .AddNewtonsoftJson(options =>
                 options.SerializerSettings.ContractResolver =
                    new CamelCasePropertyNamesContractResolver());
      .....
      .....
      
      

      如果是API比使用

       services.AddControllersWithViews().AddNewton ......
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-17
        • 1970-01-01
        • 2011-07-17
        • 1970-01-01
        • 2018-06-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多