Reputation: 139
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");
// }
}
}
Upvotes: 0
Views: 1363
Reputation: 21
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.
Upvotes: 1
Reputation: 28290
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.
Upvotes: 2