英文:
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 演示:
更多信息,请参阅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<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 demo
More information you can refer to Filters in ASP.NET Core.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论