Get Current Controller in Actionhttp://forums.asp.net/t/1501110.aspx/1?Get+Current+Controller+in+ActionThu, 14 Jan 2010 17:22:53 -050015011103551000http://forums.asp.net/p/1501110/3551000.aspx/1?Get+Current+Controller+in+ActionGet Current Controller in Action <p>Hello,</p> <p>I'm in the current controller &quot;HomeController&quot;, how do I get it on my action? This yields nothing [:(]<br> </p> <p>Function Index() As ActionResult<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim value As ControllerBuilder<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>value = ControllerBuilder.Current</b><br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return View()<br> End Function</p> <p><br> </p> <p>-imperialx<br> </p> 2009-12-07T03:22:13-05:003551042http://forums.asp.net/p/1501110/3551042.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>You want the instance of the controller? That's the &quot;this&quot; object in C#, or the &quot;Me&quot; object in VB.NET.</p> 2009-12-07T03:51:14-05:003551061http://forums.asp.net/p/1501110/3551061.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>Under the View page, I can specify if I'm within the current controller like,</p> <p>&lt;%If Not (TypeOf ViewContext.Controller Is ProjectName.HomeController) Then%&gt; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;%Html.RenderAction(&quot;ControllerName&quot;, &quot;ViewName&quot;)%&gt;<br> &nbsp;&lt;%End If%&gt;</p> <p>How can I specify if I'm under an Action?<br> </p> 2009-12-07T03:58:36-05:003551070http://forums.asp.net/p/1501110/3551070.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>Dont know whether its works for you but you can easily send your action name in a ViewData like ViewData[&quot;ActionName&quot;] = &quot;YourActionName&quot;&nbsp; to your view.</p> <p>hope this helps.<br> </p> 2009-12-07T04:05:28-05:003551084http://forums.asp.net/p/1501110/3551084.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>How about referencing a strongly-typed? </p> <p>This too yields nothing, [:(]<br> </p> <p>Dim value As String = Me.ControllerContext.RequestContext.HttpContext.Items(&quot;Home&quot;)</p> <p><br> </p> 2009-12-07T04:10:01-05:003551093http://forums.asp.net/p/1501110/3551093.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>What are you trying to accomplish? What do you mean if you're &quot;under an action&quot;? </p> <p>If you're asking how to get the name of the current controller and action from the View, you can do that with something like this <pre class="prettyprint">Dim action as String = ViewContext.Controller.ValueProvider(&quot;action&quot;).RawValue Dim controller as String = ViewContext.Controller.ValueProvider(&quot;controller&quot;).RawValue</pre><br> <br> </p> 2009-12-07T04:15:46-05:003551141http://forums.asp.net/p/1501110/3551141.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p></p> <blockquote><span class="icon-blockquote"></span> <h4>CodeHobo</h4> If you're asking how to get the name of the current controller and action from the View, you can do that with something like this </blockquote> <p></p> <p>But I'm getting this error &quot;<b>Reference to a non-shared member requires an object reference.</b>&quot;<br> <br> &nbsp;&nbsp;&nbsp; Function Index() As ActionResult<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <b>Dim action As String = ViewContext.Controller.ValueProvider(&quot;action&quot;).RawValue<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim controller As String = ViewContext.Controller.ValueProvider(&quot;controller&quot;).RawValue</b><br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return View()<br> &nbsp;&nbsp;&nbsp; End Function</p> <p><br> </p> 2009-12-07T04:39:24-05:003551195http://forums.asp.net/p/1501110/3551195.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>That code was getting the name of the controller and action in the View. To find the names of both in the controller use this:<pre class="prettyprint">Function Index() As ActionResult Dim controllerName As String = CType(Me.ValueProvider(&quot;controller&quot;).RawValue, String) Dim actionName As String = CType(Me.ValueProvider(&quot;action&quot;).RawValue, String) Return View() End Function</pre><br> </p> <p><br> <br> </p> 2009-12-07T05:11:10-05:003552941http://forums.asp.net/p/1501110/3552941.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>FYI - Please use <b>Me.RouteData.Values(&quot;action&quot;)</b> and <b>Me.RouteData.Values(&quot;controller&quot;)</b> for this instead of going through the ValueProvider.&nbsp; The RouteData contains information on the request / route (including <i>controller </i>and <i>action</i>), and the value provider contains information used for binding.</p> 2009-12-08T00:02:27-05:003557671http://forums.asp.net/p/1501110/3557671.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p></p> <blockquote><span class="icon-blockquote"></span> <h4>levib</h4> Please use <b>Me.RouteData.Values(&quot;action&quot;)</b> and <b>Me.RouteData.Values(&quot;controller&quot;)</b> for this instead of going through the ValueProvider.</blockquote> <p></p> <p>Why do I get this error &quot;Object Type is not reference&quot; when I put it on a constructor?</p> <p>Public Sub New()<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ViewData(&quot;ThisIsTheCurrentController&quot;) =&nbsp; Me.RouteData.Values(&quot;controller&quot;)<br> &nbsp;&nbsp;&nbsp; End Sub</p> 2009-12-10T06:22:25-05:003557694http://forums.asp.net/p/1501110/3557694.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>The constructor is too early to access the RouteData property (or any request-specific property, for that matter) on the controller.&nbsp; It might be better to override OnActionExecuting() and access ViewData &#43; RouteData from there, depending on what exactly you want to do.<br> </p> 2009-12-10T06:31:19-05:003617233http://forums.asp.net/p/1501110/3617233.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p></p> <blockquote><span class="icon-blockquote"></span> <h4>levib</h4> It might be better to override OnActionExecuting() and access ViewData &#43; RouteData from there, depending on what exactly you want to do.</blockquote> <p></p> <p>Hi Levib,</p> <p>How do I pass the return value back to controller? This one is not right.<br> </p> <p><u>Class</u><br> </p> <p>Public Class UserTimeCheckAttribute<br> &nbsp;&nbsp;&nbsp; Inherits ActionFilterAttribute<br> <br> &nbsp;&nbsp;&nbsp; Public Overloads Overrides Sub OnActionExecuting(ByVal filterContext As ActionExecutingContext)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim VisitController As String = filterContext.RouteData.Values(&quot;controller&quot;)<br> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br> &nbsp;&nbsp; &nbsp;<b>Return &quot;User visit Controller: &quot; &amp; VisitController &amp; &quot; at Time: &quot; &amp; Date.Now</b> [:(]<br> <br> &nbsp;&nbsp;&nbsp; End Sub<br> <br> End Class<br> </p> <p><br> <u>Controller</u></p> <p>&nbsp;&nbsp; &lt;UserTimeCheck()&gt; _<br> &nbsp;&nbsp;&nbsp; Public Class BaseController<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Inherits System.Web.Mvc.Controller<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Public Sub New()<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Sub<br> <br> &nbsp;&nbsp; &nbsp;Function Index() As ActionResult<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ViewData(&quot;Message&quot;) = &quot;Welcome to Asp.net MVC&quot;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Return View()<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End Function<br> <br> &nbsp;&nbsp;&nbsp; End Class</p> <p><br> </p> <p>-imperialx<br> </p> 2010-01-14T10:41:16-05:003617930http://forums.asp.net/p/1501110/3617930.aspx/1?Re+Get+Current+Controller+in+ActionRe: Get Current Controller in Action <p>The OnActionExecuting() method doesn't return anything.&nbsp; But you can access <b> filterContext.Controller.ViewData</b> from within that method, so you can populate ViewData.<br> </p> 2010-01-14T17:22:53-05:00