相关疑难解决方法(0)

有条件地禁用ASP.NET MVC控制器

有条件地禁用ASP.NET MVC控制器的最佳方法是什么?

如果web.config中的某些值为"true",我希望能够访问控制器操作,如果它是"false",则我想访问404

我应该写自己的属性吗?

更新:寻找比动作过滤器属性更优雅的解决方案(能够将非常量参数传递给属性构造函数)

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public class CloseForSomeSettingAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            bool mySettingValue = MySettingManager.GetMySettingValue();

            if (mySettingValue)
            {
                filterContext.Result = new HttpStatusCodeResult(404);
            }
            else
            {
                base.OnActionExecuting(filterContext);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net asp.net-mvc asp.net-mvc-3

14
推荐指数
1
解决办法
1万
查看次数

标签 统计

.net ×1

asp.net ×1

asp.net-mvc ×1

asp.net-mvc-3 ×1

c# ×1