C# 如何通过代码禁用MVC控制器?

C# 如何通过代码禁用MVC控制器?,c#,.net,asp.net-mvc,C#,.net,Asp.net Mvc,我正在为我的Web服务器使用。 我有一个控制器,我想在启动时根据命令行参数通过代码启用/禁用它 在MVC中有没有一种简单的方法可以做到这一点 现在,我在控制器的代码中考虑在禁用此配置时返回HTTP NotFound状态代码,有什么更好的主意吗?您可以执行重定向操作,将用户带到另一个控制器解释发生了什么 即: 您可以创建一个属性,将其应用于控制器,并在启动时设置该属性的静态属性,并在设置标志时拒绝访问(或返回“未找到”)。您可以使用自定义的操作过滤器来修饰控制器 public class Conf

我正在为我的Web服务器使用。 我有一个控制器,我想在启动时根据命令行参数通过代码启用/禁用它

在MVC中有没有一种简单的方法可以做到这一点


现在,我在控制器的代码中考虑在禁用此配置时返回HTTP NotFound状态代码,有什么更好的主意吗?

您可以执行重定向操作,将用户带到另一个控制器解释发生了什么

即:


您可以创建一个属性,将其应用于控制器,并在启动时设置该属性的静态属性,并在设置标志时拒绝访问(或返回“未找到”)。

您可以使用自定义的
操作过滤器来修饰控制器

public class ConfigActionFilter : ActionFilterAttribute {   
  // This method is called before a controller action is executed.
  public override void OnActionExecuting(ActionExecutingContext filterContext) {
    if(someConfigSetting) {
      filterContext.Result = new RedirectToRouteResult("Error", someRouteValues);
    }
  }
  ...
}
用法:

[ConfigActionFilter]
public class MyController : Controller {
  ...
}

更多。

或者,您可以实现自定义授权属性并将其放在控制器上

public class AuthorizationAdminAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (/*check for argument*/)
            {
                return false;
            }

            return true;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (AuthorizeCore(filterContext.HttpContext))
            {
                // ** IMPORTANT **
                // Since we're performing authorization at the action level, the authorization code runs
                // after the output caching module. In the worst case this could allow an authorized user
                // to cause the page to be cached, then an unauthorized user would later be served the
                // cached page. We work around this by telling proxies not to cache the sensitive page,
                // then we hook our custom authorization code into the caching mechanism so that we have
                // the final say on whether a page should be served from the cache.

                HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
                cachePolicy.SetProxyMaxAge(new TimeSpan(0));
                cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);
            }
            else
            {                  
                filterContext.Result = new HttpNotFoundResult();
            }
        }

        private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
        {
            validationStatus = OnCacheAuthorization(new HttpContextWrapper(context));
        }
    }
public class AuthorizationAdminAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (/*check for argument*/)
            {
                return false;
            }

            return true;
        }

        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (AuthorizeCore(filterContext.HttpContext))
            {
                // ** IMPORTANT **
                // Since we're performing authorization at the action level, the authorization code runs
                // after the output caching module. In the worst case this could allow an authorized user
                // to cause the page to be cached, then an unauthorized user would later be served the
                // cached page. We work around this by telling proxies not to cache the sensitive page,
                // then we hook our custom authorization code into the caching mechanism so that we have
                // the final say on whether a page should be served from the cache.

                HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache;
                cachePolicy.SetProxyMaxAge(new TimeSpan(0));
                cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */);
            }
            else
            {                  
                filterContext.Result = new HttpNotFoundResult();
            }
        }

        private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus)
        {
            validationStatus = OnCacheAuthorization(new HttpContextWrapper(context));
        }
    }