The constructor is too early to access the RouteData property (or any request-specific property, for that matter) on the controller. It might be better to override OnActionExecuting() and access ViewData + RouteData from there, depending on what exactly
you want to do.
Marked as answer by ricka6 on Jan 14, 2010 06:05 PM
It might be better to override OnActionExecuting() and access ViewData + RouteData from there, depending on what exactly you want to do.
Hi Levib,
How do I pass the return value back to controller? This one is not right.
Class
Public Class UserTimeCheckAttribute
Inherits ActionFilterAttribute
Public Overloads Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)
Dim VisitController As String = filterContext.RouteData.Values("controller")
The OnActionExecuting() method doesn't return anything. But you can access
filterContext.Controller.ViewData from within that method, so you can populate ViewData.
Marked as answer by ricka6 on Jan 14, 2010 06:05 PM
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Get Current Controller in Action
Dec 10, 2009 06:31 AM|LINK
The constructor is too early to access the RouteData property (or any request-specific property, for that matter) on the controller. It might be better to override OnActionExecuting() and access ViewData + RouteData from there, depending on what exactly you want to do.
imperialx
Member
523 Points
1222 Posts
Re: Get Current Controller in Action
Jan 14, 2010 10:41 AM|LINK
Hi Levib,
How do I pass the return value back to controller? This one is not right.
Class
Public Class UserTimeCheckAttribute
Inherits ActionFilterAttribute
Public Overloads Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)
Dim VisitController As String = filterContext.RouteData.Values("controller")
Return "User visit Controller: " & VisitController & " at Time: " & Date.Now [:(]
End Sub
End Class
Controller
<UserTimeCheck()> _
Public Class BaseController
Inherits System.Web.Mvc.Controller
Public Sub New()
End Sub
Function Index() As ActionResult
ViewData("Message") = "Welcome to Asp.net MVC"
Return View()
End Function
End Class
-imperialx
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Get Current Controller in Action
Jan 14, 2010 05:22 PM|LINK
The OnActionExecuting() method doesn't return anything. But you can access filterContext.Controller.ViewData from within that method, so you can populate ViewData.