2

Out of pure curiosity, is it possible to access the current controller from a static context while it is being executed with the current HttpRequest/Action?

TheCloudlessSky
  • 18,608
  • 15
  • 75
  • 116
  • I'd suggest summarizing your comments on used2could's answer in your question, it's helpful to have some context other than "curiosity" :) – Daniel Schaffer Jan 02 '11 at 05:00
  • @Daniel - Understood :) But if you follow those links, they're the exact context I was talking about. I figured this wasn't even possible since there can obviously be many controllers executing at any point, but I just thought I'd ask. – TheCloudlessSky Jan 02 '11 at 17:19

2 Answers2

1

No, this is not possible from a static context because many different controllers could be executing at some given point of time for multiple concurrent requests.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I figured as much - I just wasn't sure if there was some special way to do this. Regardless, I realized my design was flawed and reworked it so that I don't need this requirement. Thanks for the confirmation! – TheCloudlessSky Jan 02 '11 at 17:20
0

I don't know of a way to do it statically but what I do for this while handling some session/authentication management I have all my controllers inherit from a custom BaseController class that inherits from the System.Web.Mvc.Controller class. In the Base Controller class I override the OnActionExecuted method.

public class BaseController : Controller
{
    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        //Your logic here

        base.OnActionExecuted(filterContext);
    }
}


public class HomeController : BaseController
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        return View();
    }


}
Brian
  • 4,974
  • 2
  • 28
  • 30
  • Thanks, but I'm making a wrapper for `ModelStateDictionary` and I'm trying to inject the "current controller" inside of it so that the controller's `ModelState` property can be used inside of a service, so this won't do. – TheCloudlessSky Jan 02 '11 at 04:55
  • See http://stackoverflow.com/questions/3873530/inject-asp-net-mvc-controller-property-into-service-layer-dependency and http://stackoverflow.com/questions/2077055/ioc-on-ivalidationdictionary-with-castle-windsor for more detail :) – TheCloudlessSky Jan 02 '11 at 04:55
  • Ah, interesting. Good luck mate! – Brian Jan 02 '11 at 18:20