开发者

When and how reliably are MVC filters called?

开发者 https://www.devze.com 2023-04-09 23:54 出处:网络
I want to setup some \'stuff\' at the start of a controller action, and tear it down after a page has been rendered. I\'ve achieved this by overriding the \'OnActionExecuting\' and \'OnResultExecuted\

I want to setup some 'stuff' at the start of a controller action, and tear it down after a page has been rendered. I've achieved this by overriding the 'OnActionExecuting' and 'OnResultExecuted' filters (I'm using MVC 2).

The big question I have at the moment is, how reliably are these two filters called?

If the answer to that isn't 100% of the time, I reckon the easiest way to progress this, would be to use some custom filters (specifying the order of them to 'first' and 'last'..开发者_开发知识库.


100% of the time. Its part of the MVC pipeline. If they didn't it would be a pretty big security hole with the AuthorizeAttribute.


If you need absolute reliability you might want to implement a clean up filter as an IExceptionFilter and have it run the same clean up code as your OnResultExecuted override does (after checking first that it hasn't already been done). Depending on what kind of "clean up" is needed, you might be able to simply use the HandleErrorAttribute, which I believe still allows OnResultExecuted to be run as it simply replaces the result with an ViewResult of the Error view.


If the built-in filters are not called relieably, why would your custom filters be any better?

The great thing about code is that, if you understand it, it tends to do exactly the same thing every time if given the same inputs.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号