.NET 5 Blazor Server App ObjectResult "Accept" header 不被尊重
.NET 5 Blazor Server App ObjectResult "Accept" header not being respected
我正在 .NET 5 Blazor 服务器应用程序中创建一个控制器,我从我的控制器返回的 OkObjectResult
总是返回 JSON,即使我设置 [=15] =].
由于在我的自定义 InvalidModelStateResponseFactory
中得到了正确的结果,我倾向于认为这可能是一个错误。
Startup.cs:
services
.AddControllers(options =>
{
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
})
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = actionContext =>
{
KeyValuePair<string, ModelStateEntry> firstModelErrorPropertyName = actionContext.ModelState.First(s => s.Value.Errors.Count > 0);
ModelError firstError = firstModelErrorPropertyName.Value.Errors.First();
ObjectResult toReturn = new ObjectResult(new ErrorResponse
{
Status = RequestStatus.Fail,
ErrorCode = ErrorCode.MissingParameter,
ErrorDescription = $"Missing POST parameter: {firstModelErrorPropertyName.Key}: {"Description here"}"
});
toReturn.StatusCode = 200;
return toReturn;
};
})
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters()
.AddXmlOptions(options =>
{
})
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumMemberConverter());
});
控制器:
/// <summary>
/// Enroll a member in IDCS services.
/// </summary>
/// <returns></returns>
[HttpPost("enroll")]
[Consumes("application/x-www-form-urlencoded")]
[FormatFilter]
public async Task<IActionResult> EnrollAsync(
[FromForm] EnrollRequest req)
{
return Ok(new
{
content = "Enroll"
});
}
我发帖后就发现了这个问题。如果我return一个定义的对象:
return Ok(new EnrollmentSuccessResponse
{
Content = "Enroll"
});
而不是匿名对象
return Ok(new
{
content = "Enroll"
});
一切正常。
我正在 .NET 5 Blazor 服务器应用程序中创建一个控制器,我从我的控制器返回的 OkObjectResult
总是返回 JSON,即使我设置 [=15] =].
由于在我的自定义 InvalidModelStateResponseFactory
中得到了正确的结果,我倾向于认为这可能是一个错误。
Startup.cs:
services
.AddControllers(options =>
{
options.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
})
.ConfigureApiBehaviorOptions(options =>
{
options.InvalidModelStateResponseFactory = actionContext =>
{
KeyValuePair<string, ModelStateEntry> firstModelErrorPropertyName = actionContext.ModelState.First(s => s.Value.Errors.Count > 0);
ModelError firstError = firstModelErrorPropertyName.Value.Errors.First();
ObjectResult toReturn = new ObjectResult(new ErrorResponse
{
Status = RequestStatus.Fail,
ErrorCode = ErrorCode.MissingParameter,
ErrorDescription = $"Missing POST parameter: {firstModelErrorPropertyName.Key}: {"Description here"}"
});
toReturn.StatusCode = 200;
return toReturn;
};
})
.AddXmlSerializerFormatters()
.AddXmlDataContractSerializerFormatters()
.AddXmlOptions(options =>
{
})
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonStringEnumMemberConverter());
});
控制器:
/// <summary>
/// Enroll a member in IDCS services.
/// </summary>
/// <returns></returns>
[HttpPost("enroll")]
[Consumes("application/x-www-form-urlencoded")]
[FormatFilter]
public async Task<IActionResult> EnrollAsync(
[FromForm] EnrollRequest req)
{
return Ok(new
{
content = "Enroll"
});
}
我发帖后就发现了这个问题。如果我return一个定义的对象:
return Ok(new EnrollmentSuccessResponse
{
Content = "Enroll"
});
而不是匿名对象
return Ok(new
{
content = "Enroll"
});
一切正常。