Determine the current controller and action?

Last post 01-14-2008 4:16 PM by aspevia. 5 replies.

Sort Posts:

  • Determine the current controller and action?

    01-14-2008, 3:11 PM
    • Loading...
    • aspevia
    • Joined on 04-25-2003, 4:36 PM
    • Boston, MA
    • Posts 30

     Hello all,

    Since this is my first post, let me first say: Great job on the framework!  OK, now to my question:

     

    I have a UserControl which I am sharing amongs several .aspx views.  In some cases I need to construct paging urls using Rob's method called GetPagingList().  However, since the UserControl is used in different views and hence different controllers and actions, I need a way to determine what is the currently executing Controller and Action.

     
    Is there a way to do this?

     

    Thanks,

     

    Trevor

     

    Trevor de Koekkoek
    Aspevia Systems
  • Re: Determine the current controller and action?

    01-14-2008, 3:36 PM
    • Loading...
    • aspevia
    • Joined on 04-25-2003, 4:36 PM
    • Boston, MA
    • Posts 30

     Well I'm going to answer my own question:

     

    Seems I can find out the action by querying the ViewContext.  So I have a method in my user control thusly:

     

        protected string GetAction()
        {
          return (string)Url.ViewContext.RouteData.Values["action"];
        }

     This works well.  However, I initially tried this:

     

        protected string GetAction()
        {
          return (string)ViewContext.RouteData.Values["action"];
        }

     

    This fails because ViewContext is null.  It is strange that the user control ViewContext is null, yet Url.ViewContext is not null.  Surely this is a bug?
     

     

     

     

    Trevor de Koekkoek
    Aspevia Systems
  • Re: Determine the current controller and action?

    01-14-2008, 4:01 PM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 4:13 PM
    • Chicago, IL
    • Posts 164

     ViewContext is not set until the RenderView method is called.  This is the same for Pages and UserControls.

    Adam Tybor -- abombss.com
  • Re: Determine the current controller and action?

    01-14-2008, 4:06 PM
    • Loading...
    • aspevia
    • Joined on 04-25-2003, 4:36 PM
    • Boston, MA
    • Posts 30

    Well this would be during the RenderView() call.  And as I mentioned, this.Url.ViewContext is set whereas this.ViewContext is null.  That doesn't make much sense. 

    Trevor de Koekkoek
    Aspevia Systems
  • Re: Determine the current controller and action?

    01-14-2008, 4:13 PM
    • Loading...
    • abombss
    • Joined on 06-27-2006, 4:13 PM
    • Chicago, IL
    • Posts 164

     Yeah might be a bug.  I just looked at reflector and ViewPage.RenderView sets the ViewContext and ViewUserControl.RenderView does not.  Looking at the Url property on ViewUserControl it is calling into the parent page to get the helper.  { return ViewPage.Url.}

    Adam Tybor -- abombss.com
    Filed under: ,
  • Re: Determine the current controller and action?

    01-14-2008, 4:16 PM
    • Loading...
    • aspevia
    • Joined on 04-25-2003, 4:36 PM
    • Boston, MA
    • Posts 30

    Thanks.

    Well at least I have something usable for now.
     

    Trevor de Koekkoek
    Aspevia Systems
Page 1 of 1 (6 items)
Microsoft Communities
Page view counter