【发布时间】:2021-05-01 03:11:00
【问题描述】:
我有以下 API 端点:
[AllowAnonymous]
[HttpPost("login")]
public async Task<IActionResult> Authenticate([FromBody] LoginViewModel credentials)
{
try
{
var result = await _facade.SomeMethodThatFailsAndThrowsCustomCode4001(credentials);
return Ok(result);
}
catch (CustomException cEx)
{
return StatusCode(4001, new { Message = cEx.FriendlyMessage ?? "" }); //Message = Our custom user friendly message
}
}
当通过 IIS 托管在外部服务器上时,这些消息可以完美返回。在 Azure 上托管时,消息未显示,这是收到的响应:
{
"Message": ""
}
我已阅读有关 Policies allowed in on-error 的信息,但这意味着我需要在 Azure 中的 API 管理中注册我的 .Net Core API,但不确定这是否有必要。
需要进行哪些配置才能允许通过托管在 Azure 中的 API 返回自定义消息?
【问题讨论】:
-
返回时状态码是 4001 吗?如果您对消息进行硬编码,例如 new { Message = "My test Message" },会发生什么?
-
当您说“托管在 Azure 上”时,您能否澄清一下您是否指的是 Azure 应用服务?
-
@tnk479 没错
-
您想使用 4001 作为有效的状态码吗?
-
@tnk479 是的,这就是我想做的事
标签: c# azure api asp.net-core