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.
Marked as answer by mad-halfling on Mar 19, 2010 08:54 AM
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)
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.
Marked as answer by mad-halfling on Mar 19, 2010 08:54 AM
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)
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Getting Controller and Action from HttpContext
Mar 18, 2010 05:06 PM|LINK
Your code seems sound. A few comments:
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.
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
Participant
1438 Points
729 Posts
Re: Getting Controller and Action from HttpContext
Mar 18, 2010 11:27 PM|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.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Getting Controller and Action from HttpContext
Mar 19, 2010 12:21 AM|LINK
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
Participant
1438 Points
729 Posts
Re: Getting Controller and Action from HttpContext
Mar 19, 2010 08:53 AM|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.