ASP.NET MVC how to disable debug routes / views during production?

I need to create some actions of the assistant controller and the views associated with it, which I would like to (conditionally?) Disable during production.

One way would be to #ifdef DEBUG pragmas around specific routes in the body of RegisterRoutes() , but that wasn't flexible at all.

The setup in web.config will be just as good, but I'm not sure how to do this.

How the created "plug-in" acts like Glimpse or Phil Haack is older than Debugging a route .

I would rather do something simple than something YAGNI ...

+6
source share
2 answers

You can also use a filter, for example, throw this class somewhere:

  public class DebugOnlyAttribute : ActionFilterAttribute { public override void OnActionExecuting( ActionExecutingContext filterContext) { #if DEBUG #else filterContext.Result = new HttpNotFoundResult(); #endif } } 

Then you can just go into the controller and decorate action methods (or entire controllers) that you do not need to appear in the version with [DebugOnly] .

You can also use filterContext.HttpContext.IsDebuggingEnabled instead of uglier #if DEBUG I would just be inclined to use the precompiler directive, since the decision will certainly not take any processor cycle in this way.

If instead you want the global filter to check everything for multiple URLs, register it as a global filter in Global.asax:

 public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new DebugActionFilter()); } 

Then you can simply check the URL or whatever you want from the list (the path to it specified here):

 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) { } } 
+9
source
 @if (HttpContext.Current.IsDebuggingEnabled) { } 

Class

 public class UrlInformation { [XmlElement(ElementName = "ActionName")] public string ActionName { get; set; } [XmlElement(ElementName = "ControllerName")] public string ControllerName { get; set; } [XmlElement(ElementName = "AreaName")] public string AreaName { get; set; } } 

Class for XML Serializaion

 [XmlTypeAttribute(AnonymousType = true)] public class clsUrlInformation { [XmlElement("Files")] public List<UrlInformation> Url { get; set; } public clsUrlInformation() { Url = new List<UrlInformation>(); } } 

XML example (specify here the debugging action method / controller / scope names)

 <?xml version="1.0" ?> <Url> <Files> <AreaName></AreaName> <ControllerName>Home</ControllerName> <ActionName>Index</ActionName> </Files> <Files> <AreaName></AreaName> <ControllerName></ControllerName> <ActionName></ActionName> </Files> </Url> 

Action filter

 public class MyActionClass : ActionFilterAttribute { public override void OnActionExecuting( ActionExecutingContext filterContext) { 

Say you have XML. XML contains information about the scope, action method name, and controller name

 var xml = @"<?xml version=""1.0"" ?> <Url> <Files> <AreaName></AreaName> <ControllerName>Home</ControllerName> <ActionName>Index</ActionName> </Files> <Files> <AreaName></AreaName> <ControllerName></ControllerName> <ActionName></ActionName> </Files> </Url>"; 

Performing XML serialization and converting XML to a list of classes.

  var serializer = new XmlSerializer(typeof(clsUrlInformation), new XmlRootAttribute("Url")); using (var stringReader = new StringReader(xml)) using (var reader = XmlReader.Create(stringReader)) { clsUrlInformation result = (clsUrlInformation)serializer.Deserialize(reader); RouteData Route = filterContext.Controller.ControllerContext.RouteData; String controller = Convert.ToString(Route.Values["controller"]); String action = Convert.ToString(Route.Values["action"]); String area = Convert.ToString(Route.DataTokens["area"]); 

Compare current action with xml to show 404

  foreach (var item in result.Url) { if (HttpContext.Current.IsDebuggingEnabled && controller == item.ControllerName && action == item.ActionName && area == item.AreaName) { filterContext.Result = new HttpNotFoundResult(); return; } } } base.OnActionExecuting(filterContext); } } 

@CristiDiaconescu: Mentioning debugging URLs in an XML file will be more flexible. WHAT FOR? Because later, you can make changes to your XML to increase / decrease / update the URL information without changing the code and without deploying the dll. Is not it?

+2
source

Source: https://habr.com/ru/post/952001/


All Articles