gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 如何在生产中禁用调试路由/ View ?

转载 作者:行者123 更新时间:2023-12-02 03:50:45 26 4
gpt4 key购买 nike

我需要创建一些辅助 Controller 操作和关联 View ,我希望能够(有条件地?)在生产中禁用它们。

一种方法是在 RegisterRoutes() 主体中围绕特定路由使用 #ifdef DEBUG 编译指示,但这一点也不灵活。

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

然后,您就可以直接进入 Controller 并装饰不需要在生产中使用 [DebugOnly] 出现的操作方法(或整个 Controller )。

您还可以使用filterContext.HttpContext.IsDebuggingEnabled而不是丑陋的#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 如何在生产中禁用调试路由/ View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18309890/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com