asp.net-mvc – ASP.NET MVC如何在生产中禁用调试路由/视图?
我需要创建一些辅助控制器操作和相关视图,我希望能够(有条件地?)在生产中禁用. 一种方法是在RegisterRoutes()体中的特定路径周围使用#ifdef DEBUG pragma,但这根本不灵活. web.config中的设置也一样好,但我不知道如何解决这个问题. 已建立的“插件”项目如Glimpse或Phil Haack的旧版Route Debugger如何做到这一点? 我宁愿做一些比YAGNI更简单的事…… 解决方法你也可以使用过滤器,例如把这个类扔到某个地方:public class DebugOnlyAttribute : ActionFilterAttribute { public override void OnActionExecuting( ActionExecutingContext filterContext) { #if DEBUG #else filterContext.Result = new HttpNotFoundResult(); #endif } } 然后你就可以直接进入控制器并使用[DebugOnly]装饰你不需要在生产中显示的动作方法(或整个控制器). 您也可以使用filterContext.HttpContext.IsDebuggingEnabled而不是uglier #if DEBUG我只是倾向于使用预编译器指令,因为决定肯定不会采用任何CPU周期. 如果您希望全局过滤器针对几个URL检查所有内容,请将其注册为Global.asax中的全局过滤器: public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new DebugActionFilter()); } 然后你可以检查URL或你想要的任何列表(这里显示的应用程序相对路径): public class DebugActionFilter : IActionFilter { private List<string> DebugUrls = new List<string> {"~/Home/","~/Debug/"}; public void OnActionExecuting(ActionExecutingContext filterContext) { if (!filterContext.HttpContext.IsDebuggingEnabled && DebugUrls.Contains( filterContext .HttpContext .Request .AppRelativeCurrentExecutionFilePath)) { filterContext.Result = new HttpNotFoundResult(); } } public void OnActionExecuted(ActionExecutedContext filterContext) { } } (编辑:鲜蔬坊站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – ASP.net MVC:在RenderAction中获
- asp.net – MVC4 – ContextDependentView – 这是什么意思
- asp.net – 登录后对Membership.GetAllUsers()的例外情况:
- asp.net – Web部署安装程序(MSI)中没有“IIS 7部署处理程序
- asp.net – 无法加载文件或程序集App_Licenses
- asp.net – App Settings和connectionStrings配置设置中连接
- asp.net-mvc – asp.net MVC应该是View-Model封装域模型吗?
- asp.net – 在asp页面中显示来自其他站点的网页
- asp.net – Request.Url.AbsoluteUri和重写的URL
- asp.net-mvc – 为什么ASP.NET MVC 4与IList for editor不能