Json
回复:
{
"ID": 1,
"Value": 10,
"User": null
}
我需要在User
方法中更改每个响应中的OnActionExecuted
值。
public override void OnActionExecuted(ActionExecutedContext context)
{
var response = context.Result;
}
但无法阅读结果和更新详细信息。
答案 0 :(得分:1)
我认为你只需要做一些演员。此代码需要一些错误处理,但另外演示了您想要的内容:
public class ViewModel
{
public int ID { get; set; }
public int Value { get; set; }
public string User { get; set; }
}
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET api/values
[HttpGet]
public IActionResult Get()
{
var vm = new ViewModel()
{
ID = 1,
Value = 10
};
return Ok(vm);
}
public override void OnActionExecuted(ActionExecutedContext context)
{
base.OnActionExecuted(context);
var result = context.Result as OkObjectResult;
var vm = result.Value as ViewModel;
vm.User = "ardalis";
}
}
结果:
{"id":1,"value":10,"user":"ardalis"}