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.
Related
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.
I'm looking for a more precise solution to handle requests for static contents by the ASP.NET lifecycle without setting runAllManagedModulesForAllRequests to true.
As far as I know is the effect of runAllManagedModulesForAllRequests = "true" that the precondition attribute of each module will be set to "".
The problem:
I have to protect static content in a subfolder of a web application against unauthorized access
To include requests to those static contents in the ASP.NET lifecycle and therefore having some events fired, I set runAllManagedModulesForAllRequests to true in web.config.
Because this solution turns the big wheel and all managed modules are affected for the whole application, I'm looking for a more adapted solution restricted to the subfolder where this behavior is required.
I need a solution for IIS6 and II7
Question 1:
The preconditon of which modules have to be resetted (precondition = "") to fire global.asax.cs events (e.g. Application_BeginRequest) for requests for static contents?
Question 2:
Is it possible to limit this request handling to requests to a single subfolder (e.g. perhaps by placing an adapted web.config in this subfolder, tweeking the main web.config, ...)
Any suggestions would be appreciated. Thanks.
Have you thought in the direction of registering a custom HttpModule for the right event of global.asax, and then enabling the HttpModule only for the sub directory using location attribute in the main web.config itself? It is just a thought of a possible solution - I havent thought through it..
<location path="subDirectoryPath">
<system.web>
<httpmodules>
<add type="MyCustomModule.Name" name="MyCustomModule" />
</httpmodules>
</system.web>
</location>
EDIT:
You may have to override your web.config, and bring in all the httpModule section in this, and then insert the custom module at the right place, with the right precondition.
This is to avoid setting runAllManagedModulesForAllRequests to true
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
In my app, I have a web.config file with an appSettings section. The appSettings section contains a number of keys that the app uses. The appSettings section also contains the file="AppSettings.config" attribute. The AppSettings.config file then contains a subset of the values from the main web.config. The idea is for the web.config to contain all the base/default settings, and then to provide overrides of the defaults in the AppSettings.config file.
According to this post (https://stackoverflow.com/a/6940086/216160), my setup ought to work (particularly : will merge (and override) settings in the .config file).
The problem I'm seeing is that its not working. I have a default value of false, which then drives some logic about displaying some beta reporting functionality (or not), and have set the AppSettings.config to override this key to 'true'. Sadly, it continues to hide the report system. But, if I change the web.config value, then the item displays.
Is it possible that the AppSettings are not getting merged? How can I test/prove what's really happening?
EDIT
It appears that there was some kind of error in the AppSettings.config file. When all is working as it should, the merge happened exactly as expected. However, I still have the issue of how to detect when the AppSettings.config file has some kind of issue. I'd tested to see if the file was valid XML (and it was), but yet, some issue remained. When I copied the functioning key from web.config, and pasted it right below the non-working key from AppSettings.config, they appeared to be identical. I expect there must be some way to throw an error in the event of a config file merge error?
I just had the same issue (configs not merging as expected), but after explicitly deleting the /bin and /obj directories from the solution-folder and performing a rebuild, everything worked as expected again, so I would suggest you try that and see how it works...
PS: Also make sure you set the file properties of the external config to 'Copy Always'. Otherwise it won't exist in the bin-directory where your running application lives.
I was able to confirm that the external app.config works with a simple project.
app.Config (in same directory as web.config)
<appSettings>
<add key="testAppConfigString" value="APP string exists!"/>
<add key="testOverrideString" value="!!OVERRIDE string exists in app.config!"/>
</appSettings>
web.config
...
<appSettings file="app.config">
<add key="testWebConfigString" value="web config string exists!"/>
<add key="testOverrideString" value="OVERRIDE string exists in web.config!"/>
</appSettings>
...
Default.aspx
...
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
web: <asp:Label runat="server" ID="lblWeb" /><br/>
app: <asp:Label runat="server" ID="lblApp" /><br/>
override: <asp:Label runat="server" ID="lblOverride" /><br/>
</asp:Content>
...
Inside the Default.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
lblWeb.Text = ConfigurationManager.AppSettings["testWebConfigString"];
lblApp.Text = ConfigurationManager.AppSettings["testAppConfigString"];
lblOverride.Text = ConfigurationManager.AppSettings["testOverrideString"];
}
The resulting page should have the following text:
web: web config string exists!
app: APP string exists!
override: !!OVERRIDE string exists in app.config!
You can access multiple config files by using WebConfigurationmanager method. add namespace:
using System.Web.Configuration;
So, to access the appSettings of
../SomeProjectFolder/Environment/Web.config, you can do:
var config = WebConfigurationManager.OpenWebConfiguration("~/SomeProjectFolder/Environment/");
string username = config.AppSettings.Settings["username"].Value;
Hope this helps.
Perhaps worth mentioning that with Web.config those connectionStrings/#configSource and appSettings/#file are relative to project directory (not target directory). That had me for awhile.
I moved the ASP.NET master files to a different directory using the Solution Explorer.
Is there a way to propagate this change to the .aspx pages so that the MasterPageFile references get updated automatically?
(This leads me to another question - if I change the namespace or name of a class is there a way to automatically update this change in all files that use this class?)
Thanks.
To avoid renaming file path, you can just set maser page in web.configs.
<pages theme="Default" masterPageFile="~/Defaut.master">
...
</pages>
Creating Master Page (MasterPage in Web.config)