为什么我的ASP.NET Web API ActionFilterAttribute OnActionExecuting不触发? [英] Why is my ASP.NET Web API ActionFilterAttribute OnActionExecuting not firing?

查看:2451
本文介绍了为什么我的ASP.NET Web API ActionFilterAttribute OnActionExecuting不触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现怎么在这里看到:<一href=\"http://www.piotrwalat.net/nhibernate-session-management-in-asp-net-web-api/\">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 assembilies,你可以检查什么是你的空间ActionFilterAttribute的命名空间。原因有两个下2 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:


  • System.Web.Http.Filters

  • System.Web.Http.Mvc

这篇关于为什么我的ASP.NET Web API ActionFilterAttribute OnActionExecuting不触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆