【发布时间】:2020-11-18 07:13:45
【问题描述】:
如何添加以下 json 序列化设置:TypeNameHandling = TypeNameHandling.All 到响应中,以便将 $type 属性添加到响应 Json..我怎样才能实现这一点
[HttpGet("GetCustomerById")]
[ProducesResponseType(typeof(CustomerBase), StatusCodes.Status200OK)]
public virtual async Task<ActionResult<CustomerBase>> GetCustomerByIdAsync(int customerId)
{
try
{
if (customerId<= 0)
{
return BadRequest("Invalid customerId in the request.");
}
_logger.LogDebug($"Getting Customer by Id : {CustomerId}");
using (var scope = _serviceScopeFactory.CreateScope())
{
var customer= await scope.ServiceProvider
.GetRequiredService<ICustomerServerApiClient>()
.GetCustomerByIdAsync(CustomerId);
return Ok(customer);
}
}
catch (Exception ex)
{
_logger.LogCritical($"Exception in Get Customer by Id: {CustomerId} " +
$"Returning a 500 to the caller. Exception message: {ex.Message}. " +
$"Stack trace: {ex.StackTrace}.");
return StatusCode(StatusCodes.Status500InternalServerError, ex.Message);
}
}
【问题讨论】:
标签: asp.net json asp.net-core-webapi