C# &引用;请求实体';s媒体类型';text/xml';此资源不支持;,

C# &引用;请求实体';s媒体类型';text/xml';此资源不支持;,,c#,json,xml,http,asp.net-web-api2,C#,Json,Xml,Http,Asp.net Web Api2,我使用C#程序成功地序列化和反序列化了一个XML文档。我现在尝试接受在HTTP请求主体中发布到REST端点的XML文档 但是,每次尝试时,我都会收到以下错误消息: "Message": "The request entity's media type 'text/xml' is not supported for this resource.", "ExceptionMessage": "No MediaTypeFormatter is available to read an object o

我使用C#程序成功地序列化和反序列化了一个XML文档。我现在尝试接受在HTTP请求主体中发布到REST端点的XML文档

但是,每次尝试时,我都会收到以下错误消息:

"Message": "The request entity's media type 'text/xml' is not supported for this resource.",
"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'XmlDocument' from content with media type 'text/xml'.",
"ExceptionType": "System.Net.Http.UnsupportedMediaTypeException",
 "StackTrace": "   at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n   at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
这是配置我正在使用的WebAPI的文件:

using System.Net.Http.Headers;
using System.Web.Http;
using FlexService.Formatters;
using Newtonsoft.Json.Serialization;

namespace FlexService
{
  public static class WebApiConfig
  {
      public static void Register(HttpConfiguration config)
      {
        // Use camel case for JSON data.
          config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new  CamelCasePropertyNamesContractResolver();
          config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
          config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
          // Formatters are handled in appropriate order, so this must be inserted
          // before the JSON formatter that would return it as a .json object
          config.Formatters.Insert(0, new PngFormatter());

          // Web API routes
          config.MapHttpAttributeRoutes();

          config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}", new { id = RouteParameter.Optional });
      }
  }
}
这是用于制作POST的控制器:

using System.Web.Http;
using Common.Models.Api;
using Core.Actions;
using FlexService.Attributes;
using Newtonsoft.Json.Linq;

namespace FlexService.Controllers
{
    [FlexRoutePrefix("orders")]
    public class OrderController : ApiController
    {
        [HttpGet, Route("{orderNumber}")]
        public ApiOrder GetInformation(string orderNumber)
        {
            return new OrderActions().GetOrderInformation(orderNumber);
        }

        [HttpPost, Route("new")]
        public System.Xml.XmlDocument Post([FromBody] System.Xml.XmlDocument     value)
        {
            return value;
        }
    }
}
简言之,我试图以XML的形式发布文本,但出于某种原因,这个程序将以text/plain而不是text/XML的形式接收文本。现在,我意识到这个问题与我是否

     Content-Type: text/xml
在我的头球,但我不知道如何修复。我假设这行代码

      config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));

我会修好的,但还没修好。有人有什么建议吗?

你的建议奏效了吗?