locked
access debug versus production compile modes RRS feed

  • Question

  • User-1997257017 posted

    In a vb.net form application, I have a page_load section of code where I would like to know not add extra code unless I need it. Basically if the application is in development or possibly test (configuration) mode, I would like to point the application to a test url. If the application is production (configuration) mode, I would like the applicaiton to point to a production url. Thus from the page_load section of code, is there a way to know what mode the application is in? If so,can you show me the code on how to accomplsih this goal?

    In addition, if the application is in production mode is there a way not to have the 'development' or test logic be accessed by the application when it is in production mode?  If so, would you show me how to accomplish this goal?

    Monday, January 7, 2019 3:41 PM

Answers

  • User-893317190 posted

    Hi wendy elizabeth,

    Which item do you right click?

    I right click my project(not the solution)  and choose properties.

    In the left , I could see Compile tab and configuration checkbox for compile. Define debug const and Define trace const.

    In addition, the conditional compile  seems to only work for trace code like

    System.Diagnostics.Trace.WriteLine ("Error in AppendData procedure.");  
    System.Diagnostics.Trace.WriteLineIf(errorFlag,   
       "Error in AppendData procedure."); 

    If you want to change the reuqest url , I suggest you could use HttpContext.Current.RewritePath method, it could change the request url,

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext.rewritepath?view=netframework-4.7.2

    then you could tell the application whether it is in production mode or development mode using web.config.

    Below is my code.

    configuration in web.config.

    <configuration>
      <appSettings>
        <add key="mode" value="development" />
      </appSettings>

    I put all the code for production in the folder production at the root of the project and all the code for development in the folder  development

    In my global.asax , I use RewritePath 

     Sub Application_BeginRequest(sender As Object, e As EventArgs)
    'get the configuration Dim mode As String = ConfigurationManager.AppSettings("mode") ' get the request path Dim url As Uri = Request.Url
    ' if it doesn't start with mode If Not url.AbsolutePath.ToLower().StartsWith("/" + mode) Then
    ' if the mode is production , change it to development If mode = "production" Then If url.AbsolutePath.ToLower().StartsWith("/development") Then Dim target As String = url.AbsolutePath.Substring(12) HttpContext.Current.RewritePath("/" + mode + target) End If End If
    ' if the mode is development , change ti to production If mode = "development" Then If url.AbsolutePath.ToLower().StartsWith("/production") Then Dim target As String = url.AbsolutePath.Substring(11) HttpContext.Current.RewritePath("/" + mode + target) End If End If End If End Sub

    I have a test page in Production folder. In the test page of Development folder , I write test page in development

    <form id="form1" runat="server">
          <h1>
              test page in production
          </h1>
        </form>

    Then I visit production page.

    The result.

    If  you want to deny access to single folder, you could also create a web.config(not the web.config at the root it will deny all the access to your project) file in the subfolder and write the following.

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <authorization>
       <deny users="*"/>
        </authorization>
      </system.web>
    </configuration>
    

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 8, 2019 5:57 AM

All replies

  • User475983607 posted

    This is generally handled by adding a appSetting node to the web.config file. 

    If you wish to read the current URL then simply...

    Uri MyUrl = Request.Url;

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.url?redirectedfrom=MSDN&view=netframework-4.7.2#System_Web_HttpRequest_Url

    Monday, January 7, 2019 4:26 PM
  • User753101303 posted

    Hi,

    Depends. The difference is really just the url for an external site ? If it depends on the current site, ypu could build a url that martches the current site. If not, a possible option would be to configure this url in the web.config file and use a web.config transforms to publish the correct value to your dev/test/prod environnement.

    Else you can use https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/how-to-compile-conditionally-with-trace-and-debug or https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.conditionalattribute?view=netframework-4.7.2 for conditional compilation but I'm not sure it's your best option here.

    Monday, January 7, 2019 4:32 PM
  • User-1997257017 posted

    When Looking at the following link: https://docs.microsoft.com/en-us/dotnet/framework/debug-trace-profile/how-to-compile-conditionally-with-trace-and-debug, 

    there is a section 'To change compile settings from the property pages dialog box', . My visual studio does not have an advance property. There is an msbuild opton , where the only option selected is 'allow this precompiled site to be updatable is selected. There is also a build section  where only build web wite as part of the solution is checked. 

    The following options not checked are: enable code analysis, and accessibility validation. Thus can you tell me how to setup the how so that I 'debug'? Also how would you setup that code in the application to use the debug option only. Would you show me the code?

    Monday, January 7, 2019 8:13 PM
  • User-893317190 posted

    Hi wendy elizabeth,

    Which item do you right click?

    I right click my project(not the solution)  and choose properties.

    In the left , I could see Compile tab and configuration checkbox for compile. Define debug const and Define trace const.

    In addition, the conditional compile  seems to only work for trace code like

    System.Diagnostics.Trace.WriteLine ("Error in AppendData procedure.");  
    System.Diagnostics.Trace.WriteLineIf(errorFlag,   
       "Error in AppendData procedure."); 

    If you want to change the reuqest url , I suggest you could use HttpContext.Current.RewritePath method, it could change the request url,

    https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcontext.rewritepath?view=netframework-4.7.2

    then you could tell the application whether it is in production mode or development mode using web.config.

    Below is my code.

    configuration in web.config.

    <configuration>
      <appSettings>
        <add key="mode" value="development" />
      </appSettings>

    I put all the code for production in the folder production at the root of the project and all the code for development in the folder  development

    In my global.asax , I use RewritePath 

     Sub Application_BeginRequest(sender As Object, e As EventArgs)
    'get the configuration Dim mode As String = ConfigurationManager.AppSettings("mode") ' get the request path Dim url As Uri = Request.Url
    ' if it doesn't start with mode If Not url.AbsolutePath.ToLower().StartsWith("/" + mode) Then
    ' if the mode is production , change it to development If mode = "production" Then If url.AbsolutePath.ToLower().StartsWith("/development") Then Dim target As String = url.AbsolutePath.Substring(12) HttpContext.Current.RewritePath("/" + mode + target) End If End If
    ' if the mode is development , change ti to production If mode = "development" Then If url.AbsolutePath.ToLower().StartsWith("/production") Then Dim target As String = url.AbsolutePath.Substring(11) HttpContext.Current.RewritePath("/" + mode + target) End If End If End If End Sub

    I have a test page in Production folder. In the test page of Development folder , I write test page in development

    <form id="form1" runat="server">
          <h1>
              test page in production
          </h1>
        </form>

    Then I visit production page.

    The result.

    If  you want to deny access to single folder, you could also create a web.config(not the web.config at the root it will deny all the access to your project) file in the subfolder and write the following.

    <?xml version="1.0"?>
    <configuration>
      <system.web>
        <authorization>
       <deny users="*"/>
        </authorization>
      </system.web>
    </configuration>
    

    Best regards,

    Ackerly Xu

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 8, 2019 5:57 AM
  • User-1997257017 posted

    I do not have the same version of Visual studio that you are displaying. I have visual studio 2010 only.

    What do you think of the following:

    #if DEBUG
    display test url
    #else
    display production url;
    #endif

    Tuesday, January 8, 2019 5:23 PM
  • User-893317190 posted

    Hi wendy elizabeth,

    Displaying url is one way to control access, it could make user less confusing (I mean its strange if you are in production mode but have a development url ) and control the access to the page you don't want the user to go if he or she doesn't know the url.

    If he or she knows the url of your page, he or she could input the url directly and access your page.

    So you had better check whether it is in production mode  before  accessing your page in begin request or other events before page load.

    Best regards,

    Ackerly Xu

    Wednesday, January 9, 2019 1:08 AM