The best way to redirect all views to/from one vie

2019-07-08 08:37发布

问题:

Is it possible to somehow route all views to one particular view? I would like to have an "Under Construction view" that is always the default view until i "flip a switch" without having to build. Until then all other controller action routes to this view.

I would love to know if i can do it in web.config or if i have to have some if/else in RegisterRoutes in Global.asax.

回答1:

you can write a custom filter attribute that reads a flag from web.config and redirect requests to under construction page.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;

namespace MyApp
{
    public class UnderConstAttribute : ActionFilterAttribute
    {
        private readonly static AppSettingsReader _reader;
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
              _reader = new AppSettingsReader();
              if(_reader.GetValue("UnderConst", typeof(bool)))
                 filterContext.HttpContext.Response.Redirect("/Underconst.html");
        }
    }
}

and you have to add key to web.config file

<appSettings><add key="UnderConst" value="false"/></appSettings>

and you have to add this filter to global action filter collection in global.asax file



回答2:

Place a file called app_offline.htm into the root of your directory and this will be displayed for all requests.

Once you remove (or rename) this file your web requests will be processed as usual again.