【发布时间】:2012-10-11 03:48:10
【问题描述】:
我正在尝试实现这里看到的内容:http://www.piotrwalat.net/nhibernate-session-management-in-asp-net-web-api/,但我的NhSessionManagementAttribute 有问题。
我在OnActionExecuting(HttpActionContext actionContext) 上设置了断点,以查看该函数是否被调用过——实际上没有。
我仔细检查了我的global.asax.cs 文件,发现我实际上正在注册ActionFilter:
GlobalConfiguration.Configuration.Filters.Add(new NhSessionManagementAttribute());
我还用属性修饰了我的控制器类本身,以及它的操作,但无济于事:
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;
}
}
为什么这个动作过滤器不会触发其中的任何事件?
【问题讨论】:
标签: c# asp.net-web-api action-filter actionfilterattribute