Post Xml request to Asp .net Core Web Api RRS feed

  • Question

  • User-1527270255 posted

    I am trying to post XMl request in Asp. Net Core and return XML data as reponse. I have tried using InputFormatters also in startup.cs page, but not able to post the XML Request. I have created a dummy project to try this. Please find below link for the same. I would be glad if any one can help me solve this issue.

    Link:  https://github.com/sangeethakn/repo-test-projects

    Thursday, June 13, 2019 12:33 PM

All replies

  • User-1038772411 posted

    Hello sangeethakn,

    Kindly refer this below link, may be this wil help you for solvin your issue


    Thank you

    Thursday, June 13, 2019 12:45 PM
  • User753101303 posted


    And knowing what happens when  your code runs coudl help. I would start by getting rid of empty catch clauses that could hide actual problems.

    You really have to deal explicitely with XML ? The purpose of Web API is to handle whole the plubming for you and serialization/deserializarion to/from json or xml is done for you...

    Thursday, June 13, 2019 12:49 PM
  • User-1764593085 posted

    Hi sangeethakn.

    I have tried your project and you need to add Deserialize code in your XDocumentInputFormatter class:

    public class XDocumentInputFormatter : InputFormatter, IInputFormatter, IApiRequestFormatMetadataProvider
            private Type currentType { get; set; }
            public XDocumentInputFormatter()
            protected override bool CanReadType(Type type)
                currentType = type;
                if (type.IsAssignableFrom(typeof(XDocument))) return true;
                return base.CanReadType(type);
            public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
                // Use StreamReader to convert any encoding to UTF-16 (default C# and sql Server).
                using (var streamReader = new StreamReader(context.HttpContext.Request.Body))
                    var xmlDoc = await XDocument.LoadAsync(streamReader, LoadOptions.None, CancellationToken.None);
                    var model = new XmlSerializer(currentType).Deserialize(xmlDoc.CreateReader());
                    return InputFormatterResult.Success(model);

    Then send the xml request like:


    Another way is that you do not need to use XDocumentInputFormatter  at all,remove it in startup and just apply the Consumes attribute to controller classes or action methods that should expect XML in the request body.

    services.AddMvc(options =>
                    options.RespectBrowserAcceptHeader = true; // false by default
                    //options.InputFormatters.Insert(0,new XDocumentInputFormatter());
    [ProducesResponseType(typeof(Student), (int)HttpStatusCode.OK)]
    [HttpPost("StudentDetails", Name = "StudentDetails")]
    public IActionResult StudentDetails([FromBody] StudentRegister request)

    Refer to https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2#input-formatters

    Best Regards,


    Friday, June 14, 2019 6:45 AM