Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return XML data forcefully on .NET Core Web API

Tags:

asp.net-core

I thought this would be simple but I'm running into an issue of forcefully returning XML data.

  • system returns json by default
  • controller that interfaces with SSRS requires xml output
  • SSRS cannot send accept header to request xml data (that I can find)
  • controller returns IActionResult

Ideally, I'm looking for a solution that can request the GET URL and the XML formats the report controller only

I was hoping I would be able to send a configuration change before outputting data normally with return Ok(obj); but have not been able to find anything.

I've tried using the Microsoft.AspNetCore.Mvc.Formatters.Xml.Extensions nuget package but it seems like an overly complicated solution for what I'm looking for.

Thank you.

like image 271
David Nguyen Avatar asked Sep 01 '26 13:09

David Nguyen


1 Answers

If you would like to restrict the response formats for a specific action you can, you can apply the [Produces] filter.

[Produces("application/xml")]
public class YoursController

The [Produces] filter will force all actions within the YoursController to return XML-formatted responses, even if other formatters were configured for the application.

For more information refer Forcing a Particular Format section.

like image 137
Sanket Avatar answered Sep 03 '26 07:09

Sanket



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!