Sharing .Net Web Configuration File Issue - asp.net

I'm having some trouble with a current project. I have a Solution with 2 separate projects in it. The first is my Project that I am publishing, which has a web.config file. The second is a data access project, which only contains .cs files, and has an app.config file, as well as a Properties folder containing a Settings.settings file.
Like so:
Solution
DataProject
app.config
Settings.settings
Code.cs
PublishProject
Code Files
Web.config
My Problem is that I want to be able to edit the app.config file settings after the solution has been published, but the app.config file gets rolled into a DataProject.dll that sits in the bin folder in PublishProject. Because of this I cannot access the app.config file to edit it post-publishing.
I have tried to setup the app.config file as a linked file that just points to the Web.config file in the other project, but the issue there was that the Settings file does not get updated with the correct values from the app.config file unless you open the settings file in Visual Studio and allow it to reload. The values in the settings file are the ones that are being used throughout the application.
Right now are only workaround is to change out the configuration and re-publish the project.

In case anyone else runs into this problem, here is the workaround that I was able to use:
Create an xml file in the PublishProject, and convert the data in the app.config into individual xml tags like so:
<configuration>
<setting name="debug" serializeAs="String">
<value>False</value>
</setting>
</configuration>
becomes
<Properties>
<debug>True</debug>
</Properties>
Once you do this, you can call the following in your code, instead of using the value from the Settings.settings file.
XmlDocument doc = new XmlDocument();
String appPath = HttpContext.Current.Server.MapPath("~").ToString() + #"/App_Data/Properties.xml";
doc.Load(appPath);
debug = Convert.ToBoolean(doc.SelectSingleNode("Properties/debug").InnerText.ToString());
Note: that I put it in the App_Data folder of the finished project because that folder is not accessible via a browser by default.

Related

Build .Net Web application based on environment

There are 3 environments through which my .Net web application goes namely Development, Release and Production with each having their own config and project setting files.
Assuming that the setting and config files for different environments are in one system, I want to create a small script or an application where the developer just mentions the environment type and the related setting and config files get loaded and then the application builds.
Can anyone guide me on this?
You can create config transforms and use them in publish profiles. For each configuration (Debug, Release, YourOwnConfig ...) there will be a file named by its configuration (Web.Debug.config, Web.Release.Config, Web.YourOwn.Config, ...)
The trick is that you have one complete config file, the original Web.Config, and the transforms just mention the differences to this file via XSLT transform syntax (once you create a new transform, there will be some examples in the file itself showing the syntax). For example, adding a transform for an appSettings key looks like:
<configuration>
<appSettings>
<add key="ClientSessionTimeout" value="100"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
That example will replace an existing ClientSessionTimeout setting by the one specified (with value="100"). Notice how the xdt:locator specifies that the key attribute will be used to localize the setting, and xdt:Transform specifies that the attributes mentioned (here: value) will be set.
If you have applicationSettings, you need to replace the setting itself:
<applicationSettings>
<WebApplication2.Properties.Settings>
<setting name="Setting" serializeAs="String"
xdt:Transform="Replace" xdt:Locator="Match(key)">
<value>Some value</value>
</setting>
</WebApplication2.Properties.Settings>
</applicationSettings>
The differences will be for example the data source settings, other environment specific settings such as URLs to web services etc.
To create those, select a configuration such as "Debug", then right-click on the Web.Config file and you will see a context menu item "Add config transform" - click it and the Web.Debug.Config transform file will be created underneath the Web.Config. Adapt it as mentioned before; copy the entire key or setting to the transform file, then add the appropriate xdt attributes as shown above.
Finally, you can use the "Publish" function (Right-Click on the web prroject to select it). A wizard opens where you can set up a publish profile. There you can mention a configuration - like "Debug", "Release", and the ones you've created earlier.
A file publish will put the files together needed to deploy the web project and additionally perform the transformation of the Web.Config by applying the appropriate transform file (e.g. Web.Release.Config). The published config will be named "Web.Config" and contains all changes.
For trouble-shooting, and to find out more about the topic, I recommend the following links:
asp net web application: add config transform grayed out
web.config transform not working
Notice also the side-bar of Stack overflow showing more related links.

Edit applicationSettings in web.config file

I have an <applicationSettings> tag in my web.config file which contains <connectionString> and other settings related to the application.
I want to ask If I want to add some more settings to the <applicationSettings> section. How can I do that?
Is it possible to edit/add tags in the <applicationSettings> section of the web.config file?
You mean at runtime? When the application is published? Sometime else? The config file can be edited within Visual Studio.. or even just good ol' notepad. You can definitely edit it, you just need to follow the appropriate XML structure provided, meaning appsetting keys use a NameValueCollection - http://msdn.microsoft.com/en-us/library/vstudio/ms228154(v=vs.100).aspx

How to publish generic file using web publish in Visual Studio?

I am using Visual Web Developer 2010 Express (I am using VS for 10 years) and I publish a web site using web publish to Azure. I want to publish a generic file lets say a .dat file that users will download. How should I add it to project so that it is published and available to download by users. I tried adding to specific folder in project, setting build to content and always copy. I can see the folder created but when I type specific URL , i got generic file or folder not found error. If I put an xml file to same folder , I can see it with direct url.
Internet Information Service does not serve the files that it does not know about. Your .dat file is not supported by IIS by default so you have to add it manually.
Add following configuration to your web.config file.
<configuration>
<system.webServer>
<staticContent>
<mimeMap fileExtension=".dat" mimeType="application/octet-stream" />
</staticContent>
</system.webServer>
</configuration>

What file extensions are blocked by default in IIS

Some files are not served off of IIS because they are typically part of the building blocks of the website itself. For ASP.NET these are files like *.cs, *.dll, *.config, *.cshtml, etc.
You can find a list of them tied up in the IIS management setting "Filter requests" here:
But if you need to programmatically access this list, it seems tough to find. Is there a good list of these default extensions?
BTW, the IIS website has info on how to enable / disable these globally here:
http://www.iis.net/configreference/system.webserver/security/requestfiltering/fileextensions
If I'm not mistaken, you'll find them in the root web.config of the machine:
%windir%\Microsoft.NET\Framework\framework_version\CONFIG
Which is also where you'll find the machine.config file.
e.g.
<add path="*.ascx" verb="*" type="System.Web.HttpForbiddenHandler" validate="True" />
REF:
Technet: working with config files
KB: Use ASP.NET to Protect File Types
As to how you'd programmatically get to it - I haven't tried. The IIS_USRS built-in group has access to it and this doc expands on it.
Hth...
Here's the list I build out of the IIS UI since I couldn't find it anywhere. Hope you find it helpful.
disallowed extensions
.asax
.ascx
.master
.skin
.browser
.sitemap
.config
.cs
.csproj
.vb
.vbproj
.webinfo
.licx
.resx
.resources
.mdb
.vjsproj
.java
.jsl
.ldb
.dsdgm
.ssdgm
.lsad
.ssmap
.cd
.dsprototype
.lsaprototype
.sdm
.sdmDocument
.mdf
.ldf
.ad
.dd
.ldd
.sd
.adprototype
.lddprototype
.exclude
.refresh
.compiled
.msgx
.vsdisco
.rules
on localhost you can alter the applicationHost.config file, a systems file that you can edit in VS and go to the requestFiltering section, change and save.

How to read a value from an application's app.config file from a asp.net web page?

Basically i have a .net application that has a directory path stored in the app.config file. this directory path outputs xml files that will be read by an asp.net web page.
Is there any way i can get the asp.net web page to read the directory path stored in the app.config file? Should i look to use the web.config file at all?
The easiest thing to do would be to replicate the directory path into the web.config file of your asp.net web application. You could put the path in the AppSettings element of the web.config file as follows:
<appSettings>
<add key="FilePath" value="d:\fileDirectory" />
</appSettings>
You can then read this value from your asp.net app using the WebConfigurationManager or the ConfigurationManager. The WebConfigurationManager is the preferred method to use since it know how to handle ASP.Net configuration inheritance (see Antonio's comment below).
You will need to insure that the windows account under which the asp.net process is running has read privileges on the specified directory where the XML files are being stored. You can adjust this using the ACL settings of the directory.
Alternatively, instead of replicating the the directory path in web.config, could try to have your asp.net app directly read the path from the app.config file of your .net app. In this case, you would need to load the contents of the file into an XDocument or use the configuration-parsing tools in .net, and then parse the file to extract the value. You would need to make sure your asp.net app has permissions to read the app.config file. And you would still need to store a path in you web.config, this time to point towhees the app.config file is located. so personally, I would just replicate the path of the xml files into the web.config file of the asp.net app.
To read a app setting from web.xml use
ConfigurationManager under System.Configuration namespace
<appSettings>
<add key="filepath" value="D:\folder"/>
</appSettings>
To read this setting
ConfigurationManager.AppSettings["filepath"].ToString()
Dim Xmldoc As New XmlDocument
Dim xmlatt As XmlAttribute
xmldoc.load("your file path")
xmlatt =xmldoc.SelectSingleNode("/configuration/appSettings/add[#key = 'keyname']/#value")
you can use the values as xmlatt.value

Resources