ASP.NET MVC生命周期 - 做每一次页面请求的一些工作 [英] ASP.NET MVC Lifecycle - Do Some Work Once Per Page Request
问题描述
我想每个执行web请求一些工作,一个.NET MVC的Web应用程序。具体而言,我想作一个日志进入一个数据库为每个页面负载。
I'd like to perform some work per web request to a .NET MVC Web Application. Specifically, I'd like to make a log entry into a DB for each page load.
执行这项工作的覆盖控制器类 Controller.Initialize()
方法不起作用,因为<一href=\"http://stackoverflow.com/questions/5425920/asp-net-mvc-controller-created-for-every-request\">new控制器与每次调用@ Html.Action()创建。因此,如果孩子的动作是从视图调用,那么我最终双记录 - 这是不是我想要的。
Performing this work in an overridden Controller class Controller.Initialize()
method doesn't work, because a new controller is created with every call to @Html.Action(). Thus, if a child action is called from a view, then I end up double logging--which isn't what I want.
我怎样才能插入一些工作纳入MVC生命周期,这样它每次执行页面请求一次?
How can I insert some work into the MVC Lifecycle, such that it executes once per page request?
推荐答案
您可以检查初始化
如果动作是一个孩子的行动。
You can check in the Initialize
if the action is a Child Action.
ControllerContext.IsChildAction
这篇关于ASP.NET MVC生命周期 - 做每一次页面请求的一些工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!