为什么我的 ASP.NET Web API ActionFilterAttribute OnActionExecuting 没有触发? [英] Why is my ASP.NET Web API ActionFilterAttribute OnActionExecuting not firing?
问题描述
我正在尝试实现这里看到的内容:http://www.piotrwalat.net/nhibernate-session-management-in-asp-net-web-api/ 但我的 NhSessionManagementAttribute
有问题.
I'm trying to implement what's seen here: http://www.piotrwalat.net/nhibernate-session-management-in-asp-net-web-api/ but I'm having an issue with my NhSessionManagementAttribute
.
我在我的 OnActionExecuting(HttpActionContext actionContext)
上设置了断点,以查看该函数是否曾经被调用过——它没有被调用过.
I've set breakpoints on my OnActionExecuting(HttpActionContext actionContext)
to see whether the function was ever being called -- it wasn't.
我仔细检查了我的 global.asax.cs
文件 &发现我实际上正在注册 ActionFilter
:
I double-checked my global.asax.cs
file & found I am in fact registering the ActionFilter
with:
GlobalConfiguration.Configuration.Filters.Add(new NhSessionManagementAttribute());
我还装饰了我的控制器类本身,以及它的属性无效的操作:
I have also decorated both my controller class itself, as well as its actions with the attribute to no avail:
public class ClientsController : ApiController {
static readonly ClientRepository repository = new ClientRepository();
[NhSessionManagement]
public IEnumerable<Client> GetAllClients() {
return repository.GetAll();
}
[NhSessionManagement]
public Client GetClient(int id) {
Client client = repository.Get(id);
if (client == null) {
throw new HttpResponseException(
new HttpResponseMessage(HttpStatusCode.NotFound)
);
}
return client;
}
}
为什么这个动作过滤器不会触发其中的任何事件?
Why would this action filter not be firing any of the events within?
推荐答案
如果您在一个同时包含 MVC 和 WebAPI 程序集的项目中工作,您能否检查一下您的 ActionFilterAttribute 的命名空间是什么命名空间.这相当令人困惑,因为两者下都有两个 ActionFilterAttributes:
If you're working in a project contains both MVC and WebAPI assembilies, could you check what's the namespace your ActionFilterAttribute's namespace. It's fairly confusing cause there are two ActionFilterAttributes under both:
- WebAPI:System.Web.Http.Filters
- MVC:System.Web.Http.Mvc
这篇关于为什么我的 ASP.NET Web API ActionFilterAttribute OnActionExecuting 没有触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!