是否可以从.NET Core中间件检索控制器的动作结果?

编程入门 行业动态 更新时间:2024-10-25 02:20:20
本文介绍了是否可以从.NET Core中间件检索控制器的动作结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public class UsersController : APIControllerBase { public UsersController() { } public Client Get() { return new Client() { ClientID = 1, // LastUpdate = I want to update this field in middleware }; } public Client Get(int id) { return new Client() { ClientID = id // LastUpdate = I want to update this field in middleware }; } }

public class SetClientLastUpdateMiddleware { private readonly RequestDelegate next; public SetClientLastUpdateMiddleware(RequestDelegate next) { this.next = next; } public async Task Invoke(HttpContext context) { await next(context); // Can I do something like below? if(context.ActionResult is Client) ((Client)context.ActionResult).LastUpdate = Date.Now; } }

请考虑以上代码.我想为每个端点应用一个处理程序.我不知道中间件是否合适.我需要获取端点的操作结果作为其原始类型并对其进行一些更新.如果中间件不是正确的方法,那么任何建议都将不胜感激.

Please consider above codes. I want to apply a handler for every endpoint. I don't know if middleware is a proper option. What I need is to get the endpoint's action result as its original type and do some update to it. If middleware is not a proper way, any advice would be appreciated.

推荐答案

是.但是您不能在控制器中创建新客户端.尝试在Client中创建一个公共变量,您可以在中间件中提供该值.在您的控制器中,您可以获取该值并将其添加到该对象.

Yes. But you can't create a NEW client in your controller. Try to create a public variable in Client which you can give the value in the middleware. And in your controller you can get this value and add to this object.

更多推荐

是否可以从.NET Core中间件检索控制器的动作结果?

本文发布于:2023-11-05 09:48:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560515.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   中间件   动作   NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!