我写了以下 ActionFilterAttribute
Imports System.Web.Mvc
Namespace Mvc.Filters
Public Class StopWatchFilter : Inherits ActionFilterAttribute
Private tStart As DateTime
Private tStop As DateTime
Public Overrides Sub OnActionExecuting( _
ByVal filterContext As ActionExecutingContext)
tStart = DateTime.Now
End Sub
Public Overrides Sub OnResultExecuted( _
ByVal filterContext As ResultExecutedContext)
tStop = DateTime.Now
filterContext.Controller.ViewData("StopWatch") = _
((tStop - tStart).TotalMilliseconds / 1000).ToString(".00")
End Sub
End Class
End Namespace
我在我的控制器上设置
''# Fixes SO code coloring
<MyApp.StopWatch()>
Function Index() As ActionResult
Return View()
End Function
但是当我尝试ViewData("StopWatch")
在我的视图中显示时,它只是空白。
我在这里错过了一些愚蠢的东西吗?
编辑:
似乎我可以在ViewData
中设置OnActinExecuting
但不能在OnResultExecuted
.
那么,我到底如何才能制作秒表ActionFilter
呢?