c# - 如何通过代码禁用 MVC Controller ?

标签 c# .net asp.net-mvc

我正在使用 SelfHost/Katana/Owin对于我的网络服务器。 我有一个 Controller ,我想在启动时根据命令行参数通过代码启用/禁用它。

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

现在我正在考虑在禁用此配置时在 Controller 的代码中返回 HTTP-NotFound 状态代码,还有更好的想法吗?

最佳答案

您可以使用自定义 Action Filter 装饰您的 Controller 。

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 {
  ...
}

更多here .

关于c# - 如何通过代码禁用 MVC Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26934960/

相关文章:

c# - while循环中的变量初始化

c# - app.config 文件未正确提供连接

.net - URLDownloadToCacheFile 失败,HRESULT '-2146697208'

c# - 如何在 Windows XP 的桌面右键菜单中添加项目

asp.net-mvc - 如何在 LINQ to SQL 中将我的对象保存回我的数据库?

Asp.NET MVC 2 路由

javascript - 如何从 Html.ActionLink 转换为链接到 Ajax 调用的按钮?

c# - 当前上下文 mvc 4 中不存在名称 ViewBag

c# - 当您应用谓词时,是否可以从 EF 集中取回 IQueryable<>?

c# - .NET:GetDeclaredProperty() 和 GetProperty() 之间有什么区别?