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.
Simply navigate to https://localhost:{PortNo}/swagger/v1/swagger.json and get much more details about the error message.
Swagger helps users build, document, test and consume RESTful web services. It can be used with both a top-down and bottom-up API development approach. In the top-down, or design-first, method, Swagger can be used to design an API before any code is written.
Launch the app and navigate to https://localhost:<port>/swagger/v1/swagger.json . The generated document describing the endpoints appears as shown in OpenAPI specification (openapi. json). The Swagger UI can be found at https://localhost:<port>/swagger .
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With