只是动作之前发生ASP.NET MVC事件被称为? [英] ASP.NET MVC event that happens just before action is called?

查看:203
本文介绍了只是动作之前发生ASP.NET MVC事件被称为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于一些数据路由设置 Thread.CurrentCulture 的价值,但我无法找到一个事件挂钩,以计算路由后触发与之前的动作方法被调用。

I want to set the value of Thread.CurrentCulture based on some route data, but I can't find an event to hook to that fires after the routes are calculated and before the action method is called.

任何想法?

推荐答案

您可以编写一个自定义的动作过滤器属性

You could write a custom action filter attribute:

public class CustomFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // This method is executed before calling the action
        // and here you have access to the route data:
        var foo = filterContext.RouteData.Values["foo"];
        // TODO: use the foo route value to perform some action

        base.OnActionExecuting(filterContext);
    }
}

然后,你可以装饰这个自定义属性的基本控制器。而且这里有一个博客文章说明的样本实现这样的过滤器。

And then you could decorate your base controller with this custom attribute. And here's a blog post illustrating a sample implementation of such filter.

这篇关于只是动作之前发生ASP.NET MVC事件被称为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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