I am unable to return result object as XML. As I understand from the documentation, JsonResult formats the given object as JSON. However ObjectResult has content negotiation built in.
Despite both Accept and Content-Type headers set in the request to application/xml, the function always serialises the response object as JSON.
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
ILogger log)
{
ResponseMessage responseObject = new ResponseMessage
{
Code = "111",
Message = "This response shall be XML formatted"
};
return new ObjectResult(responseObject);
}
I found the post Can Azure Functions return XML? which mentions that proper content negotiation is coming, so I assume that after 5 years in .Net Core 3.1 shall support this.
EDIT:
The code I was testing was run locally. Just noticed that the same code put into hosted Azure Function via "Develop in Portal" works perfectly and returns expected XML when Accept:application/xml is set in the request. Accept is sufficient condition, Content-Type does not need to be set.