Removing formatters in ASP.NET Core

Removing formatters in the ASP.NET Core is different than it was on the tradicional ASP.NET MVC. Learn how can you do it.


Before ASP.NET Core release, if you needed to remove a formatter, we would have to use all of this code:

services.Configure<MvcOptions>(options =>
        options.OutputFormatters.RemoveAll(formatter =>
        formatter.Instance is XmlDataContractSerializerOutputFormatter));

Fortunately, people behind this project are concerned about making our life better, so they try to change it to have one easier and more friendly way to remove it.

services.Configure<MvcOptions>(option =>
{
    option.OutputFormatters.RemoveType<XmlDataContractSerializerOutputFormatter>();
});

To do this, please include the following dependency in your ‘project.json’ to be able to reference using Microsoft.AspNet.Mvc.Formatters;

"Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final"

You can see the discussion on github about this change in here.


© 2022. All rights reserved.

Powered by Hydejack v6.3.0