[英]Is CancellationToken available in ASP.NET Core ActionFilter?
I could inject CancellationToken
into ASP.NET Core action method, but I would instead prefer to work with it using action filter.我可以将
CancellationToken
注入ASP.NET 核心操作方法,但我宁愿使用操作过滤器来处理它。 How to get access to cancellation token while implementing IAsyncActionFilter
?如何在实现
IAsyncActionFilter
时访问取消令牌? My method should not have it as a parameter then.我的方法不应该将它作为参数。
You already post a link to a very good article, which contains a small hint where you can get this token.您已经发布了一篇非常好的文章的链接,其中包含一个小提示,您可以在其中获取此令牌。
MVC will automatically bind any
CancellationToken
parameters in an action method to the HttpContext.RequestAborted token, using theCancellationTokenModelBinder
.MVC 将使用
CancellationTokenModelBinder
自动将操作方法中的任何CancellationToken
参数绑定到HttpContext.RequestAborted令牌。
So, all you have to do is acquire that token in your action filter:因此,您所要做的就是在您的操作过滤器中获取该令牌:
public class CustomActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext context)
{
var cancellationToken = context.HttpContext.RequestAborted;
// rest of your code
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.