开发者

Is it possible to statically access the current controller?

Ou开发者_StackOverflow社区t 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?


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.


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();
    }


}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜