ASP.NET MVC:行为过滤器设置控制变量? [英] ASP.NET MVC: Action Filter to set up controller variables?

查看:119
本文介绍了ASP.NET MVC:行为过滤器设置控制变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,从而与每一个页面请求我必须检查特定的ID的presence的会话。如果发现我必须从数据库中抓取一个相关的对象,并将其提供给控制器。如果没有找到会话ID,我需要将用户重定向(会话过期)。

I have a scenario whereby with every page request I must check the session of the presence of a particular ID. If this is found I must grab a related object from the database and make it available to the controller. If no session ID is found I need to redirect the user (session expired).

目前,我有code(几行),这是否在我的控制器中的每一个动作方法的开头的自定义块 - 这似乎是不必要的重复。

At the moment I have a custom chunk of code (couple of lines) that does this at the start of every action method within my controller - which seems like unnecessary repetition.

这是值得场景的行为过滤器的?

Is this scenario worthy of an Action Filter?

感谢

更新
一些伟大的信息在这里的家伙。谢谢

UPDATE Some great info here guys. Thank you

推荐答案

是的,这听起来像一个动作过滤器的很好的应用,你可以在控制器级别适用于它的所有操作进行操作。你也可以把它的控制器基类的一部分,如果你不想它手动添加到所有控制器,或者自己写控制器工厂自动这一行动过滤器适用于每个控制器。

Yes, this sounds like a good application of an action filter, as you can apply it at the controller level to operate on all actions. You could also make it part of a controller base class, if you didn't want to add it to all controllers manually, or write your own controller factory which automatically applies this action filter to each controller.

请参阅ASP.NET从自定义操作MVC通滤波器对象行动从一个动作过滤器将数据传递到动作。

See ASP.NET MVC Pass object from Custom Action Filter to Action for passing data from an action filter to an action.

这篇关于ASP.NET MVC:行为过滤器设置控制变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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