Wednesday, October 31, 2018

Allow .NET Core service controller to return custom XML payload without throwing 406 Not Acceptable?

Hi all,

Using .NET Core 2.1 and trying to build a simple web service controller that processes the raw HTTP request and returns a custom response. To be more specific, I have a few SOAP methods from legacy systems that perform simple calls I want to just take the incoming SOAP XML, parse it using .NET Core's XML parser and return a response. I'm not interested in writing pages of code or using a SOAP library for .NET Core to accomplish this. This task at hand just doesn't require the overhead.

In my controller:

 [HttpPost] [Route("api/SoapRequest")] public async Task<string> SoapTest() { string response = ""; Response.Headers.Clear(); Response.ContentType = @"application/soap+xml; charset=utf-8"; //string result; using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8)) { string soapBody = reader.ReadToEnd(); response = BuildSOAPResponse(soapBody); } return response; } 

And the server accepts the incoming XML, builds my SOAP payload but on return I get this nasty "406 Not Acceptable"

HTTP/1.1 406 Not Acceptable < Content-Type: application/soap+xml; charset=utf-8 < Server: Kestrel < X-SourceFiles: =?UTF-8?B?YzpccHJvamVjdHNcZG90bmV0XE1HMlJlYWxtQVBJQWRhcHRlclxNRzJSZWFsbUFQSUFkYXB0ZXJcYXBpXGJvZHlUeXBlc1xSZWFkU3RyaW5nRGF0YU1hbnVhbA==?= < X-Powered-By: ASP.NET < Date: Wed, 31 Oct 2018 22:47:11 GMT < Content-Length: 0 

In Startup.cs I'm explicitly telling ASP.NET to ignore HttpNotAcceptable (if I understand the flag correctly) I've also explored adding the StringOutputFormatter() which doesn't work. Also the generic XML Formatter fails as it doesn't like the "application/soap+xml..." Content-Type.

 public void ConfigureServices(IServiceCollection services) { // services.AddMvc(); //(o => o.InputFormatters.Insert(0, new RawRequestBodyFormatter())); services.AddMvc(options => { options.ReturnHttpNotAcceptable = false; // If you need to add support for XML // options.OutputFormatters.Add(new StringOutputFormatter()); }); } 

Does anyone know of a simple fix to this? Should I write my own custom OutputFormatter? I can't find any examples of how to do that. Thanks!

Allow .NET Core service controller to return custom XML payload without throwing 406 Not Acceptable? Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team