简体   繁体   English

在 ASP.NET Core 3.1 Web API 的情况下模型绑定停止工作

[英]Model binding stopped working in case of ASP.NET Core 3.1 Web API

I have an ASP.NET Core 3.1 Web API with the following code in Startup.cs in which case model-binding worked perfectly without any issues:我在Startup.cs中有一个带有以下代码的 ASP.NET Core 3.1 Web API,在这种情况下,模型绑定可以完美运行,没有任何问题:

Startup.cs : Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddNewtonsoftJson(newtonsoft =>
    {
        newtonsoft.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
}

I did a minor change to encapsulate all the methods and updated the code to the following: post the below change Model binding stopped working.我做了一个小的更改来封装所有方法并将代码更新为以下内容:发布以下更改模型绑定停止工作。

Startup.cs : Startup.cs

public virtual void ConfigureServices(IServiceCollection services) => services.AddControllers().Services.AddCustomConfigureOptions();

public static IServiceCollection AddCustomConfigureOptions(this IServiceCollection services) => services.ConfigureOptions<ConfigureJsonOptions>();

ConfigureJsonOptions.cs : ConfigureJsonOptions.cs

public class ConfigureJsonOptions : IConfigureOptions<MvcNewtonsoftJsonOptions>
{
    private readonly IWebHostEnvironment webHostEnvironment;
    public ConfigureJsonOptions(IWebHostEnvironment webHostEnvironment) => this.webHostEnvironment = webHostEnvironment;

    public void Configure(MvcNewtonsoftJsonOptions options)
    {
        if (webHostEnvironment.IsEnvironment(C.Constants.Local))
        {
            // Pretty print the JSON in development for easier debugging.
            options.SerializerSettings.Formatting = Formatting.Indented;
        }

        // Parse dates as DateTimeOffset values by default. You should prefer using DateTimeOffset over
        // DateTime everywhere. Not doing so can cause problems with time-zones.
        options.SerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        // Output enumeration values as strings in JSON.
        options.SerializerSettings.Converters.Add(new StringEnumConverter());
    }
}

Can anyone help me to resolve this issue?谁能帮我解决这个问题?

Well the first thing I'm noticing is that you are never setting your webHostEnvironment property of the configure class.那么我注意到的第一件事是你永远不会设置配置类的webHostEnvironment属性。 The thing is, you are still building your service definitions (note that you have IServiceCollection instead of IServiceProvider , and IServiceCollection.ConfigureOptions<TConfigureOptions>() is really meant to extend the options pattern , which is IMO better used for dynamic settings that service configuration.问题是,您仍在构建服务定义(请注意,您有IServiceCollection而不是IServiceProvider ,并且IServiceCollection.ConfigureOptions<TConfigureOptions>()确实是为了扩展选项模式,这是 IMO 更好地用于服务配置的动态设置.

Instead of encapsulating this in a separate class, I'd say keep it simple and just stick with the lambda based config.与其将它封装在一个单独的类中,我会说保持简单并坚持使用基于 lambda 的配置。 If you have to base it off of environment, use something like this .如果您必须将其基于环境,请使用类似这样的东西。 If not, I've always found preprocessor directives to address this issue very cleanly:如果没有,我总是发现预处理器指令可以非常干净地解决这个问题:

// Startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
      services.AddControllers();
      services.AddNewtonsoftJson(jsonSettings =>
      {
        #if DEBUG
        jsonSettings.SerializerSettings.Formatting = Formatting.Indented;
        #endif

        // Parse dates as DateTimeOffset values by default. You should prefer using DateTimeOffset over
        // DateTime everywhere. Not doing so can cause problems with time-zones.
        options.SerializerSettings.DateParseHandling =
          DateParseHandling.DateTimeOffset;

        jsonSettings.SerializerSettings.ReferenceLoopHandling =
          ReferenceLoopHandling.Ignore;

        // Output enumeration values as strings in JSON.
        jsonSettings.SerializerSettings.Converters.Add(new StringEnumConverter());
      });

      // other service definitions here
      // ...
    }

This checks to see if the build was done in debug or release mode, and only includes that line in debug builds.这将检查构建是否在调试或发布模式下完成,并且仅在调试构建中包含该行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.Net Core - Model 绑定通用 Model 在升级到核心 3.0 后停止工作 - Asp.Net Core - Model Binding For Generic Model Stopped Working After Upgrade To Core 3.0 ASP.NET Core Web API 模型绑定行为更改 - ASP.NET Core Web API Model binding behavior change asp.net core web api 2中的抽象类模型绑定 - Abstract class model binding in asp.net core web api 2 Asp.net Core 3.1 Web api 对象数组请求未绑定 - Asp.net Core 3.1 Web api request with array of objects not binding 具有集合属性的类在作为 XML 发布到 ASP.Net Core 3.1 Web API 时未正确绑定 - Class with Collection Properties are not binding properly when posted as XML to ASP.Net Core 3.1 Web API ASP.NET Core 列表模型绑定向后兼容旧版 ASP.NET Web API - ASP.NET Core list model binding backward compatibility with legacy ASP.NET Web API ASP.NET 内核 3.1 Web Api 对 Z20F35E630DAF449DBFA4C3F6885 级别的授权 - ASP.NET Core 3.1 Web Api authorization on model property level ASP.NET Core 3.1:Web API 身份登录 - ASP.NET Core 3.1: Web API identity sign in Asp.net 核心 3.1 保护 API 和 web 应用程序 - Asp.net core 3.1 securing API and web app ASP.NET 内核中的全局异常处理程序 Web API 3.1 - Global Exception Handler in ASP.NET Core Web API 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM