How to retrieve web.config setting in web application subfolder - asp.net

I have a subfolder in my web application. It's not a separate application, just a subfolder, but it has its own web.config. There is an aspx page in that subfolder which needs to access a setting in the web.config file in its own folder.
When I call ConfigurationManager.GetSection("settingname") from a static initializer on that aspx page, it returns null. Why might this happen? Could it be that my setting name is wrong or that the application root web.config is being accessed instead of the subfolder's web.config?

If you want programmatic access to ConnectionStrings, AppSettings or anything else in a web.config file other than in the root of the application then you need to use the WebConfigurationManager class in the System.Web.Configuration namespace (see http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx) instead of the ConfigurationManager class in the System.Configuration namespace. This should resolve your problem.
From MSDN-
"Using WebConfigurationManager is the preferred way to work with
configuration files related to Web applications. For client
applications, use the ConfigurationManager class."
I understand (although I can't cite this as fact) that the ConfigurationManager class was designed for Windows Forms applications where I believe there is only a single App.config file.

Related

How to configure Custom Http Module at Directory level?

I created a custom http module and want to add this module to the web config. The web application is a project that contains several "sub applications". A sub application is just a folder, and within that folder it has its own web.config. I'm doing this so each application has its own application related contents.
Now I created a custom http module. When adding this to the root web.config, the module is working properly. When adding the http module config to the directory-level web.config (e.g. /Applications/MyApplication/web.config) the module is not initialized anymore.
This is the root config which is working fine.
<httpModules>
<add name="MyFirstHttpModule" type="CustomModule.CustomModule,CustomModule" />
</httpModules>
Kindly help
ASP.net configuration already supports configuration inherance. You just have to add a new web.config in the specified folder.
ASP.NET website's Web.config is part of an inheritance chain. Your website's subfolders can have Web.config. This allows for setting general settings at the site level and overriding them when necessary. Any settings in the base Web.config that aren't overridden in the subfolder stay in effect, so the "child" Web.config can be pretty small. You can continue to nest them, so sub-sub-subfolders can get their own Web.config if needed.
A good example is the Web.config file in an ASP.NET MVC application's View folder which does things like preventing directly viewing the View templates
You can read more here.
If a request is received for a file in the SubDir1 directory that does
not exist, ASP.NET begins to search the configuration hierarchy,
starting at the most local Web.config file (which is located in the
current directory, if it exists, or a parent directory). ASP.NET is
searching for an error Element for customErrors (ASP.NET Settings
Schema) element where the statusCode attribute equals "404". Once
ASP.NET finds the configuration setting for the 404 error, the URL in
the redirect attribute is returned as the response.

Is it possible to override both appSettings and connectionStrings in a Local.config file?

We presently override appSettings from our web.config in a Local.config file for each developer. However we also need to override connection strings, so we access our local copy on our machine while the web.config might reference the production server. I know that you can override appSettings by specifying file="Local.config", but is this possible for the connectionStrings as well? We already make use of an external file for the connection strings but this file has all three connection strings (local developer, dev, and production).
What I'd like to do is have the connectionString defaulted to production, but overridden on a developer's machine and on the development server. However, this doesn't seem to be possible as unlike appSettings, you can't specify a value for connectionStrings when you tell it to use an external file.
Is this possible to achieve without having to add additional code?
I should note that I cannot use the Config Transformations at the moment as we are on ASP.NET 3.5.
In the main application configuration file, you use the configSource attribute to specify the fully qualified name and location of the external file. This example refers to an external configuration file named connections.config.
<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings configSource="connections.config"/>
</configuration>
For detailed information, please visit this link on msdn (section: Using External Configuration Files)

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

HttpModule over multiple sites

I have custom redirection implemented in a module. Do I have to modify each config for all sites or is there a more general approach?Also, I have files in App_Data for this module, which would cause replication.
Thank you
Try this: (I am assuming you know. how to create a dll project, registering an Assembly in GAC, and modify the machine.config)
put your redirection-code in seperate dll project.
Register the assembly in the GAC.
Reference that assembly in all the projects/ applications where you need.
Open the machine.config and put the configs in machine.config in httpmodule section.just like you did in web.config.
Now it applies on all the applications(dotnet) where you would need.

Making a web service reference configuarble - ASP.NET?

We inherited some code that makes use of a web service on an external server from a third party. Currently all the references are done in the project directly and thus compiled to that location and such. This issue we have is that there is a test and production server for the web service with different URLs.
Is there a simple means to make the web reference more dynamic so that it can be defined in a web.config and not require changing the actual source and recompiling to switch between servers?
You could put the url in the appSettings section of the web.config file:
<appSettings>
<add key="wsUrl" value="http://example.com/staging.asmx" />
</appSettings>
And then read the value and in the constructor of the webservice proxy class and assign it to the Url property:
public SomeProxy()
{
Url = ConfigurationManager.AppSettings["wsUrl"];
}

Resources