// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Mvc.Formatters.Xml.Internal
{
///
/// A implementation which will add the
/// XML serializer formatters to .
///
public class MvcXmlSerializerMvcOptionsSetup : IConfigureOptions
{
private readonly ILoggerFactory _loggerFactory;
///
/// Initializes a new instance of .
///
/// The .
public MvcXmlSerializerMvcOptionsSetup(ILoggerFactory loggerFactory)
{
if (loggerFactory == null)
{
throw new ArgumentNullException(nameof(loggerFactory));
}
_loggerFactory = loggerFactory;
}
///
/// Adds the XML serializer formatters to .
///
/// The .
public void Configure(MvcOptions options)
{
// Do not override any user mapping
var key = "xml";
var mapping = options.FormatterMappings.GetMediaTypeMappingForFormat(key);
if (string.IsNullOrEmpty(mapping))
{
options.FormatterMappings.SetMediaTypeMappingForFormat(
key,
MediaTypeHeaderValues.ApplicationXml);
}
options.OutputFormatters.Add(new XmlSerializerOutputFormatter(_loggerFactory));
options.InputFormatters.Add(new XmlSerializerInputFormatter(options));
}
}
}