문제

So I've come to understand that MVC doesn't really have a forms PageLoadevent equivalency so where do I put a code that I would like to execute every time a page loads? I'd like to check for a cookie.

도움이 되었습니까?

해결책

Put it in the Constructor of the MVC Controller.

Or like this:

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    base.OnActionExecuting(filterContext);
    // check for cookies!
}   

다른 팁

I think this might fire for every action on the page if there are multiple actions on the page (for example partial views).

If you only want it to fire once you many need to check for

filterContext.IsChildAction

Like this

        protected override void OnActionExecuting(ActionExecutingContext filterContext) {
            if (filterContext.IsChildAction) return;

            // check for cookies!
        }

Mentioned here

In ASP.NET MVC 3, what is filterContext.IsChildAction?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top