Dynamically reading app.settings in asp.net - 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.

Related

Kind of configuration settings can be made in XML files (web.config)?

As we know there are some config setting we can make to web.config file like mentioned at link
Now as a beginner i need to know what else we can do with this file/ i.e what else we can configure with this file in ASP.NET MVC2,3?
You can configure many other items in the web.config. However this question is not direct enough to provide an answer.
You can see the entire schema here
Web.Config is not limited to any application. Web.config can work verymuch in same way for any applications. Along with the mentioned details we can include a bunch of sections into Web.Config. some of them includes
1) If you are using any services, then service end points you can define in web.config.
2) Error/Exception handling configuraton you can define.
3) if you will use Unity, you can define Register types in config.
4) Any IIS configuratons, can overridden in web.config.
5) Any IIS Error page setup.

How can I add location elements programmatically to the web config?

I have an application which creates page routes from a database. My whole site is secured with forms authentication but I need to allow unauthenticated uses to access these routes. I don't want to hard-code <location> tags for the routes in the web.config as this will negate me using a database to generate the routes.
Can anyone help?
Thanks everyone. I've found an answer here
Basically it involves creating a folder for each route and putting a web.config file in it allowing access. This approach needs to be coupled with setting RouteExistingFiles to false so that the routes don't get confused with the folders.
Rather than using strongly typed configuration classes, why not make the modifications directly in XML?
Here's an abbreviated snippet to demonstrate the concept from some code of mine that performance IIS tuning in the machine.config. The principal is the same for other XML config files though. You just need to create the appropriate XPath statements to do what you need.
XmlDocument machineConfigFile = new XmlDocument();
machineConfigFile.Load(MachineConfigPathString);
XmlNode autoConfig = machineConfigFile.SelectSingleNode(#"/configuration/system.web/processModel/#autoConfig");
autoConfig.Value = "false";
machineConfigFile.Save(MachineConfigPathString);
When saved, the XmlDocument object will preserve all other untouched document nodes. Very handy. It works great for modifying the machine.config. The only possible issue I can see is that your application will probably reset when you save your changes to the web.config. So test it out in a safe environment with a backup of your web.config just in case the reset causes any undesired outcomes!
I found this MSDN link for you. I didn't find whether you can modify the config of running server instance this way though.
Have you considered implimenting your site security in a different way? Having a portion of the site that allows unauthenticated access and a portion that does not. I am "assuming" (bad) that you are using MVC since you are describing routes - this is very easy to do with both MVC and traditional web form applications.

Asp.net Web.config Returns null after Publishing my application

After i publish my web.config i get null reference to all the code that uses the webconfig.
What could be happening?
I checked my web.config property and its set to Embedded Resource. Is this how its supposed to be.
Help people.
All of my applications have the web.config file with a Build Action of Content and not Embedded Resource. Try changing this and see if that helps.
It should be set to Content (this is the default by the way).

Multiple Web.Config files in ASP.NET web application

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.

App_Code and web.config

Let's say I have a class (MyClass.cs) in App_Code (using a ASP.NET web site, not project). There's no namespace assigned to the class.
How can I get that class to be valid in the web site .aspx pages? WHAT do I need to put in the web.config file and WHERE do I need to put it?
Do I <add assembly tag or do I <add type ??
Since the assembly tag requires version, culture, and public key, I'm not sure what those values are at compile time.
Do I just add a type tag? Where do I put it in the web.config?
EDIT:
Ok, I found part of my answer. I was getting the error because "http://localhost/MyFolder" was not set as an "application" in IIS. However, I have a BUNCH of folders, "http://localhost/MyFolder2, http://localhost/MyFolder3, etc...
New question: Is there any way to NOT have MyFolder be an application, and still make it run correctly? I've heard of a "codesubdirectories" tag, is that useful and where would I put it? Thanks.
In a web site, no name spaces are needed within your App_code folder. Nothing needs to go into your web.config to reference classes in your app_code folder.
This is one of those poorly documented aspects of dynamic compilation that cropped up when asp.net 2.0 web sites were first introduced.
An actual answer for your specific question though will require that you tell us more about what exactly you are trying to do with the class from App_Code. Most of the time you just don't have to worry about the namespace or assembly name to use those classes in your pages. You just use the class name and the compiler will figure it out and hook everything up for you.
The biggest exceptions I've run into with this are when using web controls that I've put into app_code. For those you need a #Register directive in the aspx page... and for that you need an assembly name and a namespace.
The files in app_code are compiled into an assembly named "__code" (note that this has TWO underscores, not one). That's what you can use when you need the assembly name for Register directives or in web.config or what not.
But, as far as I know, you will not be unable to use a class in register directives or some web.config settings that require a namespace unless you have explicitly wrapped that class in a namespace block.
Check the properties of the code file in Solution Explorer.
I had a situation where one of the .cs code files had "Content" selected for the Build Action. All the other files in App_Code were compiling fine but not this one.
Changed the Build Action to "Compile" and it started working as expected.
Going by your edit, have you tried moving the App_Code folder to the root of the site?
/App_Code
/MyFolder
/MyFolder2
/MyFolder3
That may address your issue.
You could also take a look at Scott Guthrie's Tip/Trick:
Creating Sub Web Projects using the VS 2005 Web Applications
Although this does use the Web Application projects, rather than web sites.
Looking at the CodeSubDirectories config element - you probably could use this - it would need to be defined in the root web.config I guess.
Also, note that the <assembly> references only need to contain version, culture and public key details if the assemblies are strongly named (and so have those values).
I don't think you have to do anything at all. That's how web site "projects" work. Anything in App_Code gets compiled.
Check to make sure the class is Public, maybe?

Resources