Reputation: 151
I have a simple ASP.NET Core API where I have a controller which was supposed to work with an input parameter of HttpRequestMessage. I tried the standard formatters and they did not work:
services.AddMvc(options =>
{
options.RespectBrowserAcceptHeader = true;
options.InputFormatters.Add(new XmlSerializerInputFormatter());
options.OutputFormatters.Add(new XmlSerializerOutputFormatter());
}
Also tried this formatter:
XmlDataContractSerializerOutputFormatter
And it did not work neither. So as result, I end up with a custom formatter:
public class RawInputFormatter : InputFormatter
{
public override Boolean CanRead(InputFormatterContext context)
{
return true;
}
public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
{
if (context == null)
throw new ArgumentNullException(nameof(context));
var request = context.HttpContext.Request;
using (var reader = new StreamReader(request.Body))
{
try
{
var content = await reader.ReadToEndAsync();
return await InputFormatterResult.SuccessAsync(content);
}
catch
{
return await InputFormatterResult.FailureAsync();
}
}
}
}
And consequently had to change the input parameter type from HttpRequestMessage to [FromBody]string
And I am being able to read the input xml after this change.
BUT
Now when I start the API (and I use Swagger), the Swagger's screen contains the error message: Failed to load API definition
And it's for all the controllers in the API I have.
I googled but did not find anything valuable. Please advise.
Upvotes: 9
Views: 3710
Reputation: 818
It would be more easy to address this problem if you would have shared your swagger configuration.
Try adding this to your swagger configuration:
options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
Upvotes: 2