Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my ASP.NET Web API ActionFilterAttribute OnActionExecuting not firing?

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.

I've set breakpoints on my OnActionExecuting(HttpActionContext actionContext) to see whether the function was ever being called -- it wasn't.

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?

like image 586
cjones26 Avatar asked Oct 20 '12 21:10

cjones26


People also ask

Does .NET Web API supports action filter?

Web API includes filters to add extra logic before or after action method executes. Filters can be used to provide cross-cutting features such as logging, exception handling, performance measurement, authentication and authorization.

What is the latest version of ASP Net Web API?

The latest ASP.NET Web API 2.2 package has the following version: "5.2. 0". You can install or update these packages through NuGet. The release also includes corresponding localized packages on NuGet.


1 Answers

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
like image 149
Troy Dai Avatar answered Sep 28 '22 04:09

Troy Dai