简体   繁体   中英

Redirect to view from global.asax not working

How to Redirect to view from global.asax?

Below is my code. It gives me HTTP Error 500.19 - Internal Server Error.

public void Application_BeginRequest(object sender, EventArgs e)
    {
        if (ConfigurationManager.AppSettings["MaintenanceMode"] == "true")
        {
            //  if (!Request.IsLocal)
            //  {
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            Server.ClearError();
            Response.Clear();

            HttpContext.Current.Response.Redirect("~/Views/Account/MaintenancePage.cshtml");
            // }
        }
    }

Add MaintenancePage GET action in your Account controller, which looks like this:

[AllowAnonymous]
[HttpGet]
public IActionResult MaintenancePage()
{
    return View();
}

And then point to this action this way to prevent infinite redirection loop:

HttpContext.Current.RewritePath("/Account/MaintenancePage");

Hope that helps.

In cases of maintenance mode, or error checking, I redirect to a static html page using Server.Transfer.

Server.Transfer("/Error.html");

I keep Error.html in the root of the project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM