向/所有视图重定向从一个视图的最佳方式(例如UnderConstructionView) [英] The best way to redirect all views to/from one view (e.g UnderConstructionView)

查看:93
本文介绍了向/所有视图重定向从一个视图的最佳方式(例如UnderConstructionView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以某种方式映射到一个特定的视图所有视图?我想有一个正在建设的意见,始终是默认视图,直到我扳动开关,而无需建立。在此之前的所有其他控制器动作路线这一观点。

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.

我很想知道如果我能在web.config中,或者如果做到这一点我必须具备一定的if / else在Global.asax中的RegisterRoutes。

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.

推荐答案

您可以编写自定义过滤器的属性,从web.config文件中读取一个标志和在建的网页请求重定向到。

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");
        }
    }
}

和你必须添加关键web.config文件

and you have to add key to web.config file

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

,你必须在Global.asax文件中添加此过滤器的全球行动过滤收集

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

这篇关于向/所有视图重定向从一个视图的最佳方式(例如UnderConstructionView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆