Last post Jan 09, 2019 01:08 AM by Ackerly Xu
Jan 07, 2019 03:41 PM|wendy elizabeth|LINK
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?
Jan 07, 2019 04:26 PM|mgebhard|LINK
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;
Jan 07, 2019 04:32 PM|PatriceSc|LINK
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/api/system.diagnostics.conditionalattribute?view=netframework-4.7.2 for conditional compilation but I'm not sure it's your best option here.
Jan 07, 2019 08:13 PM|wendy elizabeth|LINK
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
Jan 08, 2019 05:57 AM|Ackerly Xu|LINK
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.");
"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,
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.
<add key="mode" value="development" />
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 ' 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)
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">
test page in production
Then I visit production page.
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.
Jan 08, 2019 05:23 PM|wendy elizabeth|LINK
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:
display test url
display production url;
Jan 09, 2019 01:08 AM|Ackerly Xu|LINK
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.