OnActionExecuting(HttpActionContext actionContext) 在 .Net 6.0 WebAPI 中不触发。

huangapple go评论63阅读模式
英文:

OnActionExecuting(HttpActionContext actionContext) doesn't fire in .Net 6.0 WebAPI

问题

我正在尝试在我的WebAPI项目上使用过滤器,但出现了一些问题,OnActionExecuting(HttpActionContext actionContext) 方法似乎没有触发。

这是过滤器类(从这个问题中提取并稍作修改):

using System.Net;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

public class MyNoActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (IfDisabledLogic(actionContext))
        {
            actionContext.Response = new HttpResponseMessage(HttpStatusCode.NotFound);
        }
        else
            base.OnActionExecuting(actionContext);
    }

    private bool IfDisabledLogic(HttpActionContext actionContext)
    {
        return false;
    }
}

我将过滤器属性添加到我的控制器中:

[Route("api/[controller]")]
[ApiController]
[MyNoActionFilter]
public class ExampleController : ControllerBase
{...}

并且我还将其添加到我的 Program.cs 文件中:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddScoped(typeof(MyNoActionFilterAttribute));
...

我尝试在 OnActionExecuting 函数中设置断点,但似乎从未触发。

我在这里漏掉了什么?

英文:

I am trying to use a filter on my WebAPI project, but for some reason the OnActionExecuting(HttpActionContext actionContext) method doesn't fire.

This is the filter class (taken from this question and modified slightly):

using System.Net;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;

public class MyNoActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (IfDisabledLogic(actionContext))
        {
            actionContext.Response = new HttpResponseMessage(HttpStatusCode.NotFound);
        }
        else
            base.OnActionExecuting(actionContext);
    }

    private bool IfDisabledLogic(HttpActionContext actionContext)
    {
        return false;
    }
}

I added the filter attribute to my controller:

[Route("api/[controller]")]
[ApiController]
[MyNoActionFilter]
public class ExampleController : ControllerBase
{...}

And I also added it to my Program.cs file:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddScoped(typeof(MyNoActionFilterAttribute));
...

I tried setting a breakpoint in the OnActionExecuting function but it never seems to be reached.

What am I missing here?

答案1

得分: 0

尝试这段代码:

public class MyNoActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        if (IfDisabledLogic(actionContext))
        {
            context.Result = new NotFoundResult();
        }
        base.OnActionExecuting(context);
    }
}

然后直接在目标操作上使用此属性:

[MyNoActionFilter]
public IEnumerable<WeatherForecast> Get()
{
    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .ToArray();
}

GIF 演示:

OnActionExecuting(HttpActionContext actionContext) 在 .Net 6.0 WebAPI 中不触发。

更多信息,请参阅ASP.NET Core 中的过滤器

英文:

Try this code:

public class MyNoActionFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (IfDisabledLogic(actionContext))
            {
                context.Result = new NotFoundResult();
            }
            base.OnActionExecuting(context);
        }
    }

Then use this attribute on the target action directly

        [MyNoActionFilter]
        public IEnumerable&lt;WeatherForecast&gt; Get()
        {
            return Enumerable.Range(1, 5).Select(index =&gt; new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }

gif demo

OnActionExecuting(HttpActionContext actionContext) 在 .Net 6.0 WebAPI 中不触发。

More information you can refer to Filters in ASP.NET Core.

huangapple
  • 本文由 发表于 2023年7月17日 16:03:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定