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

前端 未结 6 1385
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 01:47

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 NhSessionManag

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 02:27

    For anyone else who comes across this, ActionFilterAttribute will not fire when calling YourController.YourAction from your UnitTest.

    [TestMethod]
    public void RevokeSiteAdmin_SessionOver()
    {
        FakeDbContext db = new FakeDbContext();
    
        YourController controller = new YourController(db);
        var result = controller.YourAction();
    
        //Some Assertions
    }
    

    In the TestMethod above, any ActionFilterAttributes on YourController.YourAction will not be called. However; if you call YourController.YourAction from a browser, your ActionFilterAttribute will be called.

    This is true for at least WebApi, but I don't know if it applies to MVC.

提交回复
热议问题