c# - 从 OnActionExecutionAsync 返回而不在 asp.net 核心中执行操作

标签 c# asp.net-core-mvc action-filter custom-action-filter onactionexecuting

这里我想从自定义 Action 过滤器返回,而不执行中的controller action method >asp.net core WEB API.

以下是我对示例 code 的要求。

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    bool valid=SomeMethod();
    if(valid)
        //executes controller action
    else
        //returns without executing controller action with the custom message (if possible)
}

我已经搜索并找到了一些相关的问题和答案,但对我没有任何帮助。

找到了这个await base.OnActionExecutionAsync(context, next); 但是它跳过了filters的剩余逻辑,直接执行controller action所以不适用于我的场景。

最佳答案

public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
    bool valid=SomeMethod();
    if(valid)
         await next();
    else
        context.Result = new BadRequestObjectResult("Invalid!");
}

关于c# - 从 OnActionExecutionAsync 返回而不在 asp.net 核心中执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54882813/

相关文章:

c# - Google 驱动器重定向 URI 不匹配以及如何从 ASP.net Core 2.0 中的谷歌驱动器获取文件列表

asp.net-mvc - 使用 ASP.NET MVC 操作过滤器呈现不同的菜单

c# - WebAPI 暂时从 OnActionExecuted 覆盖 JsonFormatter

c# - 使用 ExcelDataReader 从特定单元格开始读取 Excel 数据

c# - WCF 缓存服务器端

c# - IEnumerable<> 在未引用的程序集中定义 - 新的 NuGet 类库项目

.net - macOS 上的 Asp.NET Core 数据库

c# - 区域布局忽略的环境变量

model-view-controller - asp.net MVC ActionFilter 用于删除结果中的空行

c# - DirectoryEntry.Children.Remove 抛出 "unspecified error"