有条件禁用ASP.NET MVC控制器 [英] Conditionally disable ASP.NET MVC Controller
问题描述
什么是禁用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:
You can also conditionally add a route that matches that controller that would result in a 404 being returned.
这篇关于有条件禁用ASP.NET MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!