[HttpPost()]
public async Task<IActionResult> UpdateStatus([FromBody] string msg)
{}
我可以添加 AddXmlSerializerFormatters
并从正文中获取我想要的对象,但这会绕过一些错误处理。有没有办法将 application/xml 读取为字符串?
StreamReader
来读取XML内容。
[HttpPost]
[Route("update-status")]
public async Task<IActionResult> UpdateStatus()
{
using (StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8))
{
string xmlContent = await reader.ReadToEndAsync();
if (string.IsNullOrWhiteSpace(xmlContent))
{
return BadRequest("Empty XML content");
}
return Ok($"Received XML content: {xmlContent}");
}
}