有条件禁用ASP.NET MVC控制器 [英] Conditionally disable ASP.NET MVC Controller

查看:213
本文介绍了有条件禁用ASP.NET MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想有一个访问控制措施,如果在web.config中的一些值是真和404,如果是假

我应该写我自己的属性?

更新: 寻找更好的解决方案比动作过滤器属性(有能力通过不恒定的参数属性构造)

  [AttributeUsage(AttributeTargets.Class,的AllowMultiple = TRUE,继承=真)
    公共类CloseForSomeSettingAttribute:ActionFilterAttribute
    {
        公众覆盖无效OnActionExecuting(ActionExecutingContext filterContext)
        {
            布尔mySettingValue = MySettingManager.GetMySettingValue();

            如果(mySettingValue)
            {
                filterContext.Result =新的HTTPStatus codeResult(404);
            }
            其他
            {
                base.OnActionExecuting(filterContext);
            }
        }
    }
 

解决方案

最简单的将可能实现自定义的行为过滤器:

<一个href="http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs" rel="nofollow">http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

您也可以有条件地添加相匹配的控制器,将导致404返回路线。

What is the best way to disable ASP.NET MVC controller conditionally?

I want to have an access to the controller actions if some value in web.config is "true" and 404 if it's "false"

Should I write my own attribute?

UPDATE: Looking for more elegant solution than action filter attribute (with an ability to pass not constant parameter to attribute constructor)

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

解决方案

The easiest would probably be to implement a custom action filter:

http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs

You can also conditionally add a route that matches that controller that would result in a 404 being returned.

这篇关于有条件禁用ASP.NET MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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