ASP.NET redirection in the base controller

I created a basic controller that overrides Initialize and sets a cookie and saves some data in the database. Here you also need to check if the cookie is set and not redirected to the error page.

Users of this site come from the repository store on another site that gives me the store ID, and I set it to a cookie. I need to make sure that the identifier is saved and if you don’t pass them an error or return to the choice of the store.

I am having trouble redirecting or displaying this error in the base controller. I even tried the controller constructor. Below is the code I'm trying to initialize to override, but is ControllerContext null at this point?

        if (StoreID == null)
        {
            View("StoreError").ExecuteResult(ControllerContext);
            return;
        }
+5
source share
3 answers

next trick ...

requestContext.HttpContext.Response.Redirect("/home/storeError");
+6
source

I would recommend creating an Action that will check the Cookies in the OnActionExecuting method and redirect to the appropriate action method.

Clear Explanation: MVC - redirection inside the constructor

+4
source

The right way, I think, would be

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

no?

+2
source

All Articles