如何拦截 MVC 应用程序中的所有控制器调用?

发布于 2025-01-07 23:14:19 字数 104 浏览 1 评论 0原文

是否有一种快速方法可以拦截 MVC-3 中的所有控制器调用?

出于记录和测试的目的,我想构建一个工具,可以拦截所有控制器调用,并记录调用哪个控制器、使用哪个消息、在什么时间调用。

Is there a quick method for intercepting all controller calls in MVC-3?

For logging and testing purposes, I'd like to build a tool that can intercept all controller calls, and log which controller was called, with which message, at what time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

ζ澈沫 2025-01-14 23:14:19

我不记得我从哪里得到这个,但我不久前在寻找类似的东西,发现一篇文章或某个地方包含这个日志过滤器:

public class LogActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Log("OnActionExecuting", filterContext.RouteData);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        Log("OnActionExecuted", filterContext.RouteData);
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        Log("OnResultExecuting", filterContext.RouteData);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        Log("OnResultExecuted", filterContext.RouteData);
    }

    private void Log(string methodName, RouteData routeData)
    {
        var controllerName = routeData.Values["controller"];
        var actionName = routeData.Values["action"];
        var message = string.Format("{0} controller: {1} action: {2}", methodName, controllerName, actionName);
        Debug.WriteLine(message, "Action Filter Log");
    }
}

要使用它,只需将其添加到 global.asax 中的全局过滤器中:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new LogActionFilter());
}

我现在去看看是否能找到来源。

编辑:找到了。这是来自这个问题

I can't remember where I got this from, but I was looking around for something similar a while back and found an article or something somewhere that contained this logging filter:

public class LogActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Log("OnActionExecuting", filterContext.RouteData);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        Log("OnActionExecuted", filterContext.RouteData);
    }

    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        Log("OnResultExecuting", filterContext.RouteData);
    }

    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        Log("OnResultExecuted", filterContext.RouteData);
    }

    private void Log(string methodName, RouteData routeData)
    {
        var controllerName = routeData.Values["controller"];
        var actionName = routeData.Values["action"];
        var message = string.Format("{0} controller: {1} action: {2}", methodName, controllerName, actionName);
        Debug.WriteLine(message, "Action Filter Log");
    }
}

To use it, just add it to the global filters in global.asax:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new LogActionFilter());
}

I'll have a look now to see if I can find the source.

Edit: Found it. It was from this question.

对岸观火 2025-01-14 23:14:19

根据站点已有的大小,您可以在框架的 Controller 类和主控制器之间的层次结构中创建一个类。

然后

public class MyBaseController : Controller {
    protected override void OnActionExecuting(ActionExecutingContext filterContext) {
        // your logging stuff here
        base.OnActionExecuting(filtercontext);
    }
}

你的控制器的其余部分可以继承这个,例如

public class HomeController : MyBaseController {
    // action methods...
}

Depending on how big the site is already, you could create a class in the hierarchy between the framework's Controller class and your main controllers.

Something like

public class MyBaseController : Controller {
    protected override void OnActionExecuting(ActionExecutingContext filterContext) {
        // your logging stuff here
        base.OnActionExecuting(filtercontext);
    }
}

Then the rest of your controllers can inherit from this, e.g.

public class HomeController : MyBaseController {
    // action methods...
}
ぺ禁宫浮华殁 2025-01-14 23:14:19

您也可以使用自己的控制器工厂并注册它:
来自:(网上有很多示例 - 在您想要的位置插入日志记录)

改编自: http://www.keyvan.ms/custom-controller-factory-in-asp-net-mvc

using System;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Routing;

namespace IControllerFactorySample.ControllerFactories
{
    public class YourControllerFactory : IControllerFactory
    {
        #region IControllerFactory Members

        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            if (string.IsNullOrEmpty(controllerName))
                throw new ArgumentNullException("controllerName");

            IController controller = Activator.CreateInstance(Type.GetType(controllerName)) as IController;

            return controller;
        }

        public void ReleaseController(IController controller)
        {
            if (controller is IDisposable)
                (controller as IDisposable).Dispose();
            else
                controller = null;
        }

        #endregion
    }
}

不要忘记在 global.asax.cs 中注册它

ControllerBuilder.Current.SetControllerFactory(
                typeof(YourControllerFactory));

You can use your own controller factory and register it as well:
From: (many example on the net - insert logging where you want)

adapted from: http://www.keyvan.ms/custom-controller-factory-in-asp-net-mvc

using System;
using System.Configuration;
using System.Web.Mvc;
using System.Web.Routing;

namespace IControllerFactorySample.ControllerFactories
{
    public class YourControllerFactory : IControllerFactory
    {
        #region IControllerFactory Members

        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            if (string.IsNullOrEmpty(controllerName))
                throw new ArgumentNullException("controllerName");

            IController controller = Activator.CreateInstance(Type.GetType(controllerName)) as IController;

            return controller;
        }

        public void ReleaseController(IController controller)
        {
            if (controller is IDisposable)
                (controller as IDisposable).Dispose();
            else
                controller = null;
        }

        #endregion
    }
}

dont forget to register it in global.asax.cs

ControllerBuilder.Current.SetControllerFactory(
                typeof(YourControllerFactory));
傲影 2025-01-14 23:14:19

有一个由 Phil Haack 开发的路由调试器

ASP.Net路由调试器

there is a routing debugger developed by Phil Haack

ASP.Net Routing Debugger

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文