asp.net web config appSettings in null in masterpage - asp.net

I have got an ASP.NET web application. In one of its folders there are several master pages and also a web.config file. The problem is that when I read appSettings in a master page page_load all of the appSettings in that web.config file are null. Instead it contains the website 's main web.config file appsettings when I try to read allkeys in ConfigurationManager.AppSettings.

You can specify the folder for the web.config you want as follows:
System.Configuration.Configuration c = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/folder");
Reading: System.Configuration.WebConfigurationManager

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.

How to retrieve web.config setting in web application subfolder

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.

asp.net application getting master page name from appSettings key

I'm trying to figure out how an asp.net application is setting the MasterPageFile from a config file appSettings key like this:
<appSettings>
<add key="MasterPageFile" value="~/Other.Master" >
</appSettings>
This causes it to ignore the Page directive's MasterPageFile attribute in the .aspx files (which point to a different master page). I have searched through the whole solution and can't find any ConfigurationManager or MasterPageFile calls that are loading this appSettings key. From what I understand, you would usually have to put this in a system.web section of a config file in a pages element with a masterPageFile attribute.
Anyway, how is it setting the master page from this appSettings key? Is there some other way to retrieve appSettings that I don't know about?
MaserPage File can be set at page level or pre_init level. Setting it at config level will override your page settings so avoid it.

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

Prevent root web.config to be overriden by a sub web.config

I have 2 web.config files at root/ and at root/Web. I want that when I hit /Root/Web my application should load a certain set of settings from the root/Web.Config even though they are present in /root/web/Web.config
Any ideas where and what should I be modifying?
Yes, the way to do that is removing the elements in sub web.config that conflict with the settings in the root web.config.
If you need them for other purposes then you need to read whatever settings you want/need and apply them programmatically.
From MSDN:
The root of the ASP.NET configuration hierarchy is a file referred to
as the root Web.config file, and it is located in the same directory
as the Machine.config file. The root Web.config file inherits all of
the settings in the Machine.config file. The root Web.config file
includes settings that apply to all of the ASP.NET applications that
run a specific version of the .NET Framework. Because each ASP.NET
application inherits default configuration settings from the root
Web.config file, you need to create Web.config files only for settings
that override the default settings.

Resources