在每次请求执行code [英] Execute Code on Every Request

查看:119
本文介绍了在每次请求执行code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够执行对每一个充塞数据到ViewData的要求有些code。目前,我有一个基本的控制器类,我所有的控制器继承和我重写OnActionExecuting做它。

I want to be able to execute some code on every request that stuffs data into ViewData. Currently I have a base controller class that all my controllers inherit from and I override OnActionExecuting and do it there.

我的这种方法唯一担心的是谁曾经创建了一个新的控制器将继承形成的基类。

My only concern with this approach is that whom ever creates a new controller will HAVE to inherit form the base class.

有没有办法在Global.asax登记的东西,就像你用自定义模型粘合剂做,那将得到跑每个请求?有点像一个全球行动过滤器或东西。

Is there a way to register something in the global.asax, like you would do with custom model binders, that would get ran every request? Kinda like a global action filter or something.

推荐答案

而不是使用基控制器类(我认为这是大多数情况下的最佳选择),你可以使用的自定义操作调用。我会从内置操作调用派生,并在您需要的额外的东西洒。您注册在Global.asax中应用程序启动时的动作调用,它可以覆盖OnActionExecuting / OnActionExecuted / OnResultExecuting / OnResultExecuted。可以,例如,使用OnResultExecuting在某些ViewData的添加。在这一点上,你就会知道完成了动作,也知道的ActionResult类型。

Rather than using a base controller class (which I think is the best option for most scenarios), you could use a custom action invoker. I'd derive from the built-in action invoker and sprinkle in the extra stuff you need. You register the action invoker during app startup in global.asax and it can override OnActionExecuting / OnActionExecuted / OnResultExecuting / OnResultExecuted. You could, for example, use OnResultExecuting to add in some ViewData. At that point you'll know the action completed and also know the type of ActionResult.

这篇关于在每次请求执行code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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