The best way to redirect all views to/from one view (e.g UnderConstructionView)

安稳与你 提交于 2019-12-11 01:37:20

问题


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.



来源:https://stackoverflow.com/questions/9129633/the-best-way-to-redirect-all-views-to-from-one-view-e-g-underconstructionview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!