【问题标题】:How to use async method in MVC filter?如何在 MVC 过滤器中使用异步方法?
【发布时间】:2018-11-17 12:52:02
【问题描述】:

其实我在MVC中通过继承AuthorizationFilterAttribute创建了一个过滤器。 将 OnAuthorization() 方法覆盖到类中。 在这个 OnAuthorization() 方法中,我尝试对另一个方法进行异步调用。 所以我的页面没有响应。 然后我切换到同步调用而不是异步。 但我认为异步对我有好处。

我尝试过 It 页面没有响应。所以我们不能在过滤器中使用异步还是有什么问题

【问题讨论】:

  • 能分享一下代码吗?

标签: asp.net-mvc


【解决方案1】:

在 ASP.NET Core 中试试这个:

public class CustomAuthorizationFilter : IAsyncAuthorizationFilter
{
     public async Task OnAuthorizationAsync(AuthorizationFilterContext filterContext)
     {
        // Do necessary staffs here
     }
}

【讨论】:

    猜你喜欢
    • 2014-10-04
    • 2018-11-10
    • 2014-08-03
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 2021-04-16
    相关资源
    最近更新 更多