Home/ASP.NET Forums/General ASP.NET/MVC/Getting Controller and Action from HttpContext

Getting Controller and Action from HttpContext [Answered]RSS

13 replies

Last post Mar 19, 2010 08:53 AM by mad-halfling

Please remember to mark replies as answers if you find them useful =8)
  • levib

    levib

    Star

    7782 Points

    1110 Posts

    Microsoft

    Re: Getting Controller and Action from HttpContext

    Mar 18, 2010 05:06 PM|levib|LINK

    Your code seems sound.  A few comments:

    Mad-Halfling

    Would the controller and action name ever not exist/get passed in?  I couldn't see any reason why not, but I was wondering - even if it were a default action, from the URL's point of view, this would still generate an action name in the RouteData, wouldn't it?

    You can get the controller and action name from filterContext (filterContext.ActionDescriptor.ActionName and filterContext.ActionDescriptor.ControllerDescriptor.ControllerName).  This should be easier than going through RouteData.  And you're guaranteed that they're set.  So that else block after the DB call isn't necessary.

    Mad-Halfling

    Also, output caching is something that's explicitly stated, isn't it?  As you suggested, and as far as I am aware, I am not explicitly using it (nor would I want to as each page in this app is always dynamic) but are there any gotchas that I can check for that might enable it somehow?

    Using [OutputCache] or calling the output caching methods are the only ways to enable it.  The first few lines where output caching is disabled is just a precaution in case an [OutputCache] inadvertently gets set on this controller.

  • Mad-Halfling

    Mad-Halfling

    Participant

    1438 Points

    729 Posts

    Re: Getting Controller and Action from HttpContext

    Mar 18, 2010 11:27 PM|Mad-Halfling|LINK

    Thanks for the feedback - I don't have ActionDescription as an available property/method on the filterContext object - all I have are Controller, Equals, GetHashCode, GetType, HttpContext, RequestContext, Result, RouteData, ToString - what am I missing? 

    Had figured that about the output cache, but wanted to check it couldn't sneak in anywhere - thanks for clarifying that.

    Please remember to mark replies as answers if you find them useful =8)
  • levib

    levib

    Star

    7782 Points

    1110 Posts

    Microsoft

    Re: Getting Controller and Action from HttpContext

    Mar 19, 2010 12:21 AM|levib|LINK

    mad-halfling

    I don't have ActionDescription as an available property/method on the filterContext object

    It's available in MVC 2 (as of Beta or RC1, I think?).  If you're using MVC 1, use RouteData.GetRequiredString() instead.  This is the same as RouteData.Values[], but it performs the null-or-empty check for you.

  • Mad-Halfling

    Mad-Halfling

    Participant

    1438 Points

    729 Posts

    Re: Getting Controller and Action from HttpContext

    Mar 19, 2010 08:53 AM|Mad-Halfling|LINK

    Ah, I was wondering about that after I logged off last night - yes I'm still on 1 (complicated app + deadline = not-upgrading-yet).  No matter, it works OK and I've put a comment in the program that it can be changed when the framework is upgraded.  Thanks for all your help, you are a gent and a scholar. 

    Please remember to mark replies as answers if you find them useful =8)
  • ‹ Previous Thread|Next Thread ›