0

How to display a default page like Maintenance page for all the views if a web.config key is set to true?

If the key is false, then show the regular views.

Plz note, I don't want to repeat the code in each controller and am looking for a common place, like _ViewStart or _Layout page where this can be defined.

Thanks.

annantDev
  • 367
  • 4
  • 20

1 Answers1

0

You could create your own ActionFilterAttribute and base Controller, which gives you access to OnActionExecuting. You could then test if the Web.Config value is set (perhaps loading it into an Application variable when you first start your Web app) and if it is set, setting the Controller and Action attributes to your maintenance page. Then all of your controllers, instead of inheriting from the standard Controller would inherit from your controller instead, except for the Maintenance controller which could still inherit from the normal Controller.

For instance:

[RedirectToMaintenancePage]
public class MyController : Controller
{
}

And for a typical controller:

public class SampleController : MyController
{
... Your actions ...
}

Then create a class called RedirectToMaintenancePageAttribute.cs:

public class RedirectToMaintenancePageAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
         If Web.config says to go to Maintenance page then...
         {
              filterContext.Result = RedirectToRouteResult("route name") // Or some other redirect
              return;
         }

         // else
         base.OnActionExecuting(filterContext);
    }
}

This is all off the top of my head, but I think it should work, and if it doesn't hopefully it'll give you some ideas.

Steve Owen
  • 2,022
  • 1
  • 20
  • 30