XmlSerializerInputFormatter is obsolete - ASP.NET Core 2.1

后端 未结 2 1378
滥情空心
滥情空心 2021-01-18 07:42

I am using the following to accept XML serialized in my Core API App.

services.AddMvc(options =>
{
    // allow xml         


        
相关标签:
2条回答
  • 2021-01-18 08:03

    According to the source code, there's a constructor that has not been marked as Obsolete:

    public XmlSerializerInputFormatter(MvcOptions options)
    

    This constructor takes an instance of MvcOptions, so you can pass through your existing options argument:

    services.AddMvc(options =>
    {
        // allow xml format for input
        options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
    }) ...
    

    As of ASP.NET Core 3.0, this constructor is the only one available. Those that were marked obsolete have now been removed.

    0 讨论(0)
  • 2021-01-18 08:03

    With .NET Core 2.2 or later XmlSerializerInputFormatter should be marked as deprecated.

    Instead a of explicitly defining XML serializers as we did before, in the .NET Core 2.2 we can add them simply by calling AddXmlSerializerFormatters() method which will do the job now. Read here why it has been deprecated

    Here is how you can do it.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc(config =>
        {
            config.RespectBrowserAcceptHeader = true;
            config.ReturnHttpNotAcceptable = true;
    
            config.OutputFormatters.Add(new CsvOutputFormatter());
        }).AddXmlSerializerFormatters().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    }
    
    0 讨论(0)
提交回复
热议问题