views:

269

answers:

3

I have created a base controller which overrides Initialize and sets a cookie and saves some data in the DB. Here I also need to check if a cookie is set and if it is not redirect to an error page.

Users of this site are coming from a store locater on another site which passes the store ID to me and I set it in a cookie. I need to make a sure an ID is stored and if not pass them to an error or back to choose a store.

I am having trouble redirecting or showing this error in the base controller. I even tried the constructor of the controller. Below is code I am trying in Initialize override, but the ControllerContext is null at this point?

        if (StoreID == null)
        {
            View("StoreError").ExecuteResult(ControllerContext);
            return;
        }
A: 

the following did the trick...

requestContext.HttpContext.Response.Redirect("/home/storeError");
A: 

proper way i think would be

  Return RedirectToAction("StoreError","Home");

no?

Fredou
+1  A: 

I would recommend to create an Action which will check the Cookie in the OnActionExecuting method and redirect to the proper action method.

kazimanzurrashid