Web.config in virtual directory is not overriding parent website's Web.config - asp.net

I've got a website which has a virtual directory that also includes a website, and when I try to launch a page from the virtual directory, I receive an IIS 7.5 error that the name (from the connection string) is already in the collection. This wasn't a problem before I added a domain user as the Identity in the app pool. Does anyone know why this is happening? I was under the impression that sub level application's web.config overrides the parent's by default. And again, this was once working until I added a custom Identity.
The error on the page is "Parser Error Message: The entry 'dbname' has already been added."
Where the dbname is in both connection strings (i.e. parent and virtual directory). I can't delete one of the connection strings because the virtual directory is only created for test purposes, but in production it runs as its own website.

A sub web.config doesn't override a parent regardless of application. All web.configs will stack up all the way to the root of the primary application. In order for sub-application folder to make use of a connection string key that is already in use it must first be removed or all connection strings must be cleared. If you want this to be a truly stand alone application as a child add this to your connection strings:
<connectionStrings>
<clear />
<your connection string>
<connectionStrings>
If you just want to remove the single connection string use this:
<remove key="yourConnectionStringName" />

#u84six,
Today this had happened to me.
I had some websites and webservices in the same root website. The root is the default iis website (Default Web Site) and i think it was pointing to C:\Inetpub\wwwroot. Its a development environment.
After i read this answer, i found out that one of my webservices was the root website (still not sure what changed it... maybe visual studio after a migration from 2010 to 2012). That webservice was using the connectionString in conflict. I change the path back to C:\Inetpub\wwwroot\ and everything went to normal.
Maybe something similar happened to you.

Related

Web.config settings not available when deploying WCF service to local IIS 10

I spent the last 2 hours trying to find an answer, unsuccessfull .... hope anyone can help.
I created a WCF service which I "deployed" to my local IIS 10 (on Windows 10).
This "deployment process" was performed by adding a new IIS web site (ASP.NET v4.6.2) in IIS manager. The physical path of this IIS site is the root of my WCF project (where SVC and web.config reside).
I can invoke the service and WSDL when calling it from SoapUI, and executiong stops at the breakpoint in my service handler method. So far, so good.
It does not see my web.config changes though. Upon debugging the code that tries to read the config values, I noticed the file path of the web.config file is: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\web.config.
It seems to me that this is the "master" config of my .NET (runtime) environment.
The projects web.config file is available next to the SVC, and afaik everyone has access in IIS and on the file system. Am I missing something?
A couple of other things:
there are web.config transforms for debug and release; both are empty, and adding the entries to the debug version manually did not help
one of the other projects in the solution has a app.config, so the service can also be ran as console or windows service application (which both work)
Thanks in advance for any help!
I discovered the problem. This was partly due to the fact that I was maintaining someone else's code and overlooked some elementary stuff.
The configuration was read through this line of code:
Configuration rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration(null);
As the variable name says, this will get the root web.config (in the .NET runtime folder).
When I made the following change:
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
it did see my settings. Afaik "~" means the root of the application, versus "/" means the root of the website (correct me if I'm wrong).
For completeness, my setting in the web.config looks like this:
<appSettings>
<add key="someFakeKey" value="someValue" />
</appSettings>
And the whole code-block to retrieve the setting:
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
if (config.AppSettings.Settings.Count > 0)
{
KeyValueConfigurationElement poliskluisServerSetting = config.AppSettings.Settings["someFakeKey"];
if (poliskluisServerSetting != null) _poliskluisserver = poliskluisServerSetting.Value;
}

UNC path in web.config

I created a set of ASP pages which receive HTTP requests. Some of these requests pull data from a database, and few of them open text files to get info. My path to these text files are pretty straight-forward on my web server, and I refer to these in my web.config as below in the appSettings node.
<add key="MasterPath" value="C:\inetpub\wwwroot\VecoXtra\" />
However, my client (who is going to use this) has set up my scripts on their server but wants to use a UNC path to these files, and the process stops when changing it to:
<add key="MasterPath" value="\\10.10.6.2\euro$\INETPUB\VeXtra\StaticFiles\" />
I read somewhere to double up the slashes, but to no avail, as I think there are some permissions issues here. I tried identity in my config and adding my windows administrator login to the application pool but nothing allows me to access these files.
I changed the Applcation Pool AND the setting to load profile to true and it kicked into gear.. thanks for letting me talk it through :)

Nesting web applications in IIS and web.config issue

Current installation
I have two web applications app_A and app_B (same app with app_A but for test purposes) under IIS default website. A domain www.mydomain.com that points to the server needs to access app_A. That can be done by changing the physical path from \inetpub\wwwroot\ to \inetpub\wwwroot\app_A.
The second application should be access under www.mydomain.com/app_B/
Problem
When accessing www.mydomain.com/app_B/ because it's now a sub-directory of app_A it sees the web.config from app_A and I got error like "duplicate entries in web.config" when accessing the www.mydomain.com/app_B/ application. I can eliminate the errors by using the tag to remove first and declare again the entries in app_B web.config.
Questions
Is there any other way to make the installation in order app_A would be access from www.mydomain.com/ and app_B from www.mydomain.com/app_B without messing the web.config files as described above?
For the current installation, is there a way to set something on IIS in order for app_B not to see at all web.config from app_A because is a subdirectory?
For the current installation, do you see any real problems (possibly on security) by using the remove tag for the app_B application?
For the current installation I observer a strange behaviour. If I login to app_A and app_B and logout from app_A it also logout from app_B (not always). I am using Active Directory for authentication. Do I need to change something in app_B's web.config in order to say that it's totally different application?
I know this is an old question and you might have found out the solution. I am replying in case you need an answer.
There are two ways to avoid merging of parent’s config file and child’s config file. Either you can add
inheritInChildApplications="false" Tag in the parent’s config file. For example:
<location path="." inheritInChildApplications="false">
<connectionStrings>
</connectionStrings>
</location>
Or
you can add "Remove" tag or "Clear" in child’s config file to clear the parent’s settings.
Also, I don’t see any security threats by clearing parents settings.
By any chance are you using same cookie name in authentication for parent and child applications? If this is the case, once you login to child application, the cookie generated by the parent application will be overridden. Try specifying name of the cookie for at least one of the application.

Can I override a connection string in the web.config for local development?

I have a WebForms project that has a connection string hard coded into the web.config (for Debug - web.Debug.config). This connection string points to a DB server for development.
I'd like to run a local copy of that database so that my changes don't immediately affect others.
What I've been doing is going into the web.config and updating the connection string to point to my local DB. This works, but is somewhat tedious as I have to remember to exclude the web.config and have to re-update it if I undo all changes.
Since, like I said, others are using this solution, I'd like to avoid checking anything in or modifying the web config.
Is there any way to override the web.config connection string to force the site to point to my local DB without checking anything in to source control?
One solution would be to use config transformation feature.
VS 2012:
Go to Build->Configuration Manager
Click on the Active solutions configuration List box and select "New".
Enter local as name of your new configuration and click Save.
Make sure you change the values under Configuration column back to Debug or which
ever was default previously
Right click on your Web.config file and select Add Config Transform, which will add the newly created web.local.config
Inside web.local.config add the local connection string transform, something like
<connectionStrings>
<add name="DbConnection"
connectionString="...Add Local Connection String..."
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
When you run or debug the project, make sure you select local configuration from the list box next to green run arrow. You just don't check in the web.local.config
One thing you might be able to do is have a separate file for your connection strings - you wouldn't check this into source control at all.
Depending on how your stuff is organized, you might need everyone who's doing development to do the same thing, and depending on how you do your publish/deploy, this may not work for you.
For example:
Web.config
<configuration>
<connectionStrings configSource="connectionstrings.config">
</connectionStrings>
</configuration>
connectionstrings.config (not in source control)
<connectionStrings>
<add name="cs" connectionString="server=.;database=whatever;"/>
</connectionStrings>
Each developer can choose which database their local machine points to, and as long as the connectionstrings.config file is not in source control (add it to the ignore list), nobody will step on each other's feet.
Create a SQL ALIAS on your local machine.
Click start run and type "cliconfg.exe" on local machine. This will help you to create a SQL Alias on your local machine. On the web.config connect to the live database but on your machine create a SQL Alias that will redirect to your local database. When this is done, when you publish the program it will automactically connect to the live database but when running from your local machine it will connect to local database without any code change.

How do you set handler mapping to a directory in IIS 6.0?

I have IIS 7.0 on my development machine and IIS 6.0 on my server. On my development machine I was able to set a handler map on a directory within my site called /ViewHtml/ and I mapped it to asp.net. In my global.asax I check the request sent to asp.net for /ViewHtml/ and I serve the appropriate html file(html version of a Doc, Power Point, or Excel file) located outside this apps virtual directory. I am doing it this way because all files are permission protected, we didn't want to put these files in are database due to scalability, and I need to hide the path to these file on the server. This all works in IIS 7.0 exactly how I would like it to. Although I have not been able to get my IIS 6.0 server configured to map all requests to that directory to asp.net.
Any ideas? Thanks Guys?
I set up a web application using the same configuration you're using and I'm also getting the 404. I don't know why it works in IIS 7, but here's what I had to do to fix it.
Create a class that implements the System.Web.IHttpHandler class. move the the code from Application_BeginRequest to your implementation of IHttpHandler.ProcessRequest.
Now you just have to register your HTTP handler with ASP.NET. To do so add an entry in your Web.config at /configuration/system.web/httphandlers.
Web.config Example:
...
<httpHandlers>
<clear />
<add verb="*" path="*" type="namespace.classname, assemblyname" />
</httpHandlers>
...
That entry is telling ASP.NET to handle HTTP requests with any extension and any HTTP method by running the code in your HTTP hander. Note that I'm also clearing all the previously definded handlers (defined in the machine's web.config).
Note that you will still need the Application Mapping configured in IIS.
If I understand the problem correctly, it sounds like you need add a "Wildcard Application Mapping" for your virtual directory. In other words, you want to forward all requests to any file extension to ASP.NET's ISAPI extension.
To do so, open the properties of your virtual directory. On the Virtual Directory tab (Home Directory tab if it's a web site), click the Configuration... button. Click the Insert... button next to the bottom list box in the dialog that shows up. In this dialog, choose "%SYSTEMROOT%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" as the executable and make sure to un-check "Verify that file exists" checkbox, since the files to be requested don't live in your virtual directory.
I hope this helps!
i think your problem is all about access policy , you have to make sure that the access policy is Read and not None , cz with None you have no permission to even read files from your website

Resources