Multiple Web.Config files in ASP.NET web application - asp.net

I have an ASP.NET web application where i am having multiple subrirectories in the root folder.in my root web.config, i have sessionMode as "StateServer" . So in one page of my subdirectory, i am not able to do serialization. If i change the SessionMode method to "InProc" , it will work fine. I want to maintain the web.config file in the root directory as it is.So i am planning about having another web.config file in sub directory.Can anyone tell me how to do this ?
Thanks in advance

While you can have a Web.config in every subdirectory not all settings are allowed at all levels.
And SessionMode is one setting that can only be made in the application-root.

You can just place a new web.config file in the sub-directory and ASP.NET will override any settings you change in that directory.

If you mark the class that is being put in the Session with the [Serializable] attribute, it can usually be used in an StateServer setup.

Just put another web.config in the subdirectory. ASP.NET allows for that, and I have several areas on my website where the web.config contains values specifically for that "application" specifically.
That said:
1) Are you sure it's a good idea to maintain state in two different ways? It would probably be better to figure out how to make your session state serializable or get rid of using session state altogether.
2) All those web.config files can get tough to maintain if you're not careful about what values you put in each.

Related

Web.config file not inheriting ALL parent Web.config settings (elmah)

I have a few applications that are set up using elmah for error handling. I recently read that Web.config files inherit from parent applications/directories, and decided to test it so that I can change elmah's settings in one place (since all the current Web.configs are identical when it comes to the elmah configuration). After creating a virtual directory in IIS7, nesting all my applications under it, and creating a stand-alone Web.config file in the physical folder, I tried testing it.
After removing the elmah specific configurations from one of the applications, I found that elmah stops working altogether. Just to see if it was something I did wrong, I created a connection string in the parent Web.config file, then called the connection string through code in the application. No issues. Inheritance was working fine. I tried adding all the elmah specific configurations back in, and removing them one at a time, but it kept complaining every time I removed something.
The question I have is:
Can only certain settings be inherited from the parent Web.config?
If not, am I just not comprehending how inheritance works with nested Web-configs?
I wanted to ask if it's an issue with me before I ask about it on the elmah site.
You should check this answer, it will maybe help you.
Also, in your child web.config, make sure that you do copy the configsections for Elmah (or make sure they are properly inherited). Otherwise, the elmah sections in your child web.config might not work (not entirely sure about that though)

Modifying Root Web.config in code

I would like to store some meta-information about a given site instance that can (a) be managed by that site instance and (b) persist clobbering of Web.config file.
The site will run in multiple environments (dev,testing,staging and production) and each environment can have different values for this metadata.
Note: All environments are running IIS 7.0+
The Root Web.config seems very appealing, as it is certainly outside of the website. Therefore, both files and databases can be changed while maintaining the metadata. I have seen how to modify the appSettings of the Web.config stored in the website, but is it possible to similarly modify the appSettings in the Root Web.config (Specifically within the proper directive)?
If you have other suggestions of approaching this problem, I would be very happy to hear them. Thank you!
would not use web.config for runtime modifications, that will cause the application to recycle, perhaps some other form of configuration file like app.config
if my assumption is incorrect and the web.config will not be edited after the application is started, then you can use WebConfigurationManager to access the file sections
Yes you can modufy the app settings within your web.config Just use the WebConfigurationManager class in the System.Web.Configuration namespace to create a Configuration object. This object could then be used to read and write changes to the web.config file.
You could then create your own "keys" or attributes that could be read as needed.
Depending upon what your attributes represent or if they need to be picked up by multiple environmnets from that server I would also look into making the modifications within the machine.config file as then those settings would apply to the enter machine and thereby picked up by multiple environments( if you are hosting multiple webapps from the server). This could save you time in modifying multiple web.config files and narrorw the storage or the metadata to one location vs. multiple config files in certain situations.

Dynamically reading app.settings in asp.net

I've moved my appsettings section outside of the web.config using:
<appSettings configSource="AppSettings.config"/>
This allows me to change my appsettings without actually restarting IIS.
I know however, that IIS monitors all configuration files constantly. How can I attach to event my-appsetting-has-changed to take some custom action upon that?
According to this reference and this reference,
[The] ASP.Net runtime does not detect when the external [config] file changes.
If that's true, then you might get some mileage out of the FileSystemWatcher, but I cannot think how to use that effectively in an ASP.NET scenario.
I hope this helps.
You can use the FileSystemWatcher class for this.

Read a web.config file in a virtual directory

I have an ASP.NET application (Root Application) that has a virtual directory set up to another ASP.NET application (Virtual Application). How can I make the Virtual Application read values from the Root Application's web.config file? I was looking at the WebConfigurationManager.OpenWebConfiguration() class, but I'm unsure how how to tell it to go up one level from the root. For example, I would tell it to go to ~/web.config to get the the Virtual Application's web.config file, but I need it to go up one more level to the Root Application's file structure. Is this even the correct approach?
Any help would be greatly appreciated!!
You can use the ExeConfigurationFileMap class with ConfigurationManager, like:
string configFile = new FileInfo(Server.MapPath("/Web.config")).Directory.Parent.FullName + "\\Web.config";
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = configFile;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
Response.Write(config.AppSettings.Settings["Test"].Value);
IIS does have programmatic access to its configuration data (which is documented on MSDN and/or Technet). This will be the only supported route (i.e. will continue to work across IIS versions).
Otherwise you can hack a solution (both of these will require higher than usual rights for the process):
Parse the output from appcmd.exe:
E.g. here:
> C:\Windows\system32\inetsrv\appcmd.exe list vdir
VDIR "Default Web Site/" (physicalPath:E:\Dev\weblocal\XYZ)
VDIR "Default Web Site/DevRoot/TestWebClient" (physicalPath:E:\Dev\Tests\ClientSideWeb)
VDIR "Default Web Site/Home" (physicalPath:E:\Data\Homepages)
Read the configuration directly from C:\Windows\System32\inetsrv\config.
I think you will find that your desired behavior is in fact the default behavior.
web.config settings cascade down. Your app will look up to the next hierarchical web.config if it can't find the value.
This would allow you to just look up via AppSettings etc. for most cases.
I'm not sure what happens if you really need to direct access to the file as opposed to the various config api access methods.
I have a setup like this right now using IIS7 with multiple virtual apps configured.
I tried HectorMac's suggestion again and it still doesn't work for me. As a result I am going to seek an alternative to storing my value in the web.config file.

Is it possible to completely negate a "higher" web.config in a subfolder?

Is it possible to completely negate a web.config in a subfolder?
Obviously, I have a web.config in the root.
If I have a subfolder called "MyApp", can I write a bunch of code in there and have it run without any reference to the web.config at root? It would have its own web.config, and wouldn't even reference the "higher" web.config in root.
What I'm looking for is complete App isolation. I'd like to be able to write an app in a subfolder of an existing site, which ignores the entire web.config hierarchy above it -- the app would an island all to itself.
I know I can use the "clear" element, but is that the best way? Just put a "clear" under every top level element? Just wondering if there's another way.
Duplicate of Will a child application inherit from its parent web.config?
On the web.config file in the root directory, wrap the <system.web> element with the following element: <location path="." inheritInChildApplications="false"></location>
Check out this link for a reference:
http://www.jaylee.org/post/2008/03/Prevent-ASPNET-webconfig-inheritance-and-inheritInChildApplications-attribute.aspx
Yes, you have to clear those sections want to override. Thinking about it a bit more, this makes sense, as the only way to clear everything might make it very hard to work out what to clear it to. Clear normally resets everything, including the root web.configs in the web.configs and machine.config defined in the frameworks /config folder on your server.
Note that you'll also lose access to the /bin folder, /app_code folder, etc. This may or may not be what you want.
Whether you can create sub-applications with your host is another matter to consider as well.
No. By design for shared hosting and simplicity sake.
IIS7 changes that a little by allowing the configs to be explicitly locked/unlocked.
It sounds more like you should create a virtual directory that is another application root entirely.
I've just come across this issue in my work and my solution was to create a new website, instead of trying to nest my application under the existing website. I kept the domain mapping the same for the new application (i.e. www.mysite.com ) but changed the port number/mapping.
As a result I can use my new app on www.mysite.com:88 and didn't have to use a subdomain.
The caveat here is that my application is a web service so having to specify the port number in the URL was a possibility for me. It might not be an option for you but I thought I would post this incase it helps someone in my situation.
The <location path="." inheritInChildApplications="false"></location> solution wasn't an option for me as the inheritInChildApplications doesn't seem to exist before ASP.Net Framework 4.0?

Resources