【发布时间】:2015-08-27 10:36:33
【问题描述】:
一个问题的几个故事
我实现了一个类 (
MyClass),它有一个超时回调的东西,它应该在响应发送到客户端后立即启动。我可以使用action filter来完成此操作,但我不想明确使用action filter(因为用户方便)。当MyClass在 Action Method 主体中实例化时,请附加Action Filter。我遇到一种情况,需要在执行操作后触发函数委托(用于跟踪超时事件)。
考虑一个类,该类旨在当实例在该
action method上启动时运行一些代码(在执行action method之后)。我需要在一个特定的动作执行它的结果后执行一些代码(我有它的动作名称)。
我知道使用custom Action Filter 并实现它的OnActionExecuted 方法是可行的方法,但我想知道的是,如何在操作时附加action filter 动态正在执行。
我的意思是,如何在不订阅Global.asax 的情况下附加action filter,也不使用[MyCustomActionFilter] 在Action Method 上方指定它。
已编辑:
考虑我有以下课程:
public class MyClass
{
public List<SomeDelegate> TimeoutCallbackFunction{get; set;}
public MyClass()
{
// I want to attach action filter here.
// user may forget to attach MyFilter filter to Action Method
// So that it would be easier for users if this handled by MyClass Constructor.
HttpContext.Current....... or something
}
}
考虑我的动作过滤器是:
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
// Fetch newly added callback delegates from session.
// Run them all.
}
所以在控制器中我有以下代码:
//I want following filter attach to be removed.
//[MyFilter]
public ActionResult MyAction()
{
// This piece of code, should attach action method with 'MyFilter' filter
// And dismiss user from nominate it above of 'MyAction' action method
MyClass myObject=new MyClass();
}
顺便说一下,“用户”是指使用我的类并基于我的 dll 开发 mvc.asp 的人。
我的问题是:
1- 是否可以将action filter 附加到当前正在执行的操作?如果是,怎么办?
2-如果不可能,那么我还有什么其他选择可以在Result Executed之后运行一些代码?
3- 如果没有其他选择,那么人们在这种情况下会怎么做?是否有任何标准化的方式来触发计时器回调?
【问题讨论】:
标签: c# asp.net-mvc actionfilterattribute