Prisoner ZERO
Prisoner ZERO

Reputation: 14176

MVC Application_BeginRequest localhost redirected you too many times

I am trying to implement a "Maintenance Mode" into my application. My thought is to "RedirectToRoute" when detected. However, I am getting the following exception:

"localhost redirected you too many times"

I've looked at various online solutions, but none work. Any help is appreciated.

GLOBAL ASAX:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    var inMaintenanceMode = false;

    bool.TryParse(ConfigurationManager.AppSettings[Settings.Application.Modes.MaintenanceMode], out inMaintenanceMode);
    if (inMaintenanceMode)
    {
        Response.RedirectToRoute("Default", new { controller = "Maintenance", action = "Index" });
        Response.End();
        return;
    }
}

CONTROLLER:
It never gets here...

public class MaintenanceController : Controller
{
    // GET: Maintenance
    public ActionResult Index()
    {
        var viewModel = new MaintenanceIndexViewModel();

        // Forces a new Session_Start attempt
        Session.Abandon();

        return View(viewModel);
    } 
}

Upvotes: 0

Views: 1228

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038930

To avoid the infinite redirect loop you could add an additional check in your Application_BeginRequest method whether the current route is the Maintenance controller and if so, skip the redirect as you have already redirected to it in the previous request.

Upvotes: 3

Related Questions