Loading a different web.config file at runtime fo ASP.NET - asp.net

I have a web application which I want to use for multiple business areas. I do not want to have multiple instance of the web app code on the web server.
So as an example: I have //MyWebSite/Virtual_Directory_1 refers to c:\Inetpub\wwwroot\MyWebApp pointing to Database_A
What I want is to setup another Virtual Directory as follows:
//MyWebSite/Virtual_Directory_2 refers to c:\Inetpub\wwwroot\MyWebApp pointing to Database_B.
The I was thinking of doing this was by having separate web.config files, but I am not sure how to actually do this.
Any help will be appreciated.
Thanks
Parag

You can split your configuration to multiple files and you can also create 1 or more virtual directories in IIS for a single Physical directory. However this means you would have 2 instance of your web application available on your web container but both utilizing the same configuration files no matter they are split on multiple files.
You can also have a web.config file for each sub directory in your web application and each file can override its parent web.config settings (Not every thing is overridable).
To create a virtual directory on the run dialogue box type inetmgr... The IIS Manager will open up. Right click on the default web site if on the client OS and select New then Virtual Directory. Virtual Directory setup wizard will appear. Follow the simple steps it has to offer and you end up creating 2 virtual directories for a single source directory.
If you just want to have different functionalities (i.e. pointing to different database) for each web app instance then consider web.config configuration sections instead.. and have database connection strings saved against a key in app settings and load the specific database connection string considering which resource is accessed.
Have a look at the article on this location:

One way of doing it I guess would be to have a shared web.config file and then have multiple connection strings in that config file that match the virtual directory name.
<connectionStrings>
<add name="VirtualDir1" connectionString="data source=BLAHBLAH" />
<add name="VirtualDir2" connectionString="data source=BLAHBLAH" />
</connectionStrings>
When getting the connection string simply reference like:
ConfigurationManager.ConnectionStrings(Request.ApplicationPath).ConnectionString
Although to be honest I think a little time spent with build projects and having multiple copies of the code with their own config files will save you pain in the long run.

Related

web.config application pool error after moving to new server

I have the following error after move my website from server to another server
I have already checked iis and make sure the app has an application pool and it points to the correct file path
here is a screenshot of my application pool
I even tried to add virtual directory and add application and still not working
Check siteMapFile attribute of sitemap
The possible reason behind the issue is:
When you create a new web application using visual studio.net, it automatically creates the virtual directory and configures it as an application. However, if you manually create the virtual directory and it is not configured as an application, then you will not be able to browse the application and may get the above error. The debug information you get as mentioned above, is applicable to this scenario. To resolve it, Right Click on the virtual directory - select properties and then click on "Create" next to the "Application" Label and the textbox. It will automatically create the "application" using the virtual directory's name. Now the application can be accessed.
When you have sub-directories in your application, you can have a web.config file for the sub-directory. However, there are certain properties that cannot be set in the web.config of the sub-directory such as authentication, session state (you may see that the error message shows the line number where the authentication or session-state is declared in the web.config of the sub-directory). The reason is, these settings cannot be overridden at the sub-directory level unless the sub-directory is also configured as an application (as mentioned in the above point). Mostly we have the practice of adding web.config in the sub-directory if we want to protect access to the sub-directory files (say, the directory is admin and we wish to protect the admin pages from unauthorized users). But actually, this can be achieved in the web.config at the application's root level itself, by specifying the location path tags and authorization.
in your case the site map section causing the issue. try to remove it from the config file.
You could refer this below link:
Nested ASP.NET 'application' within IIS inheriting parent config values?

Error 404 on /bin browsing

I run IIS 7.5 on Windows Server 2008 R2.
I'm getting a 404: file not found error when I browse the "/bin" folder. I understand that this is a security policy by Microsoft.
I tried doing what is written in here but it didn't solve the issue. Any ideas?
EDIT:
Should be noted that I have no security issues. I run the IIS for direcotry browsing in a private network. Eventually I need to perform crawling and indexing on it. The problem is that pages under "/bin" and '/AppConfig" are not being crawled because of the 404 error.
I just need a solution for this issue. Again, no security issues are relevant here.
Bin folder is not intended as a place where a developer should put web pages.
In IIS 7.5 you can configure
Open Internet Information Services (IIS) Manager
In the Connections pane, go to the connection, site, application, or directory for
which you want to modify your request filtering settings.
In the Home pane, double-click Request Filtering.
In the Request Filtering pane, click the Hidden Segments tab
Select the relative path that you want to show (BIN folder), and then click Remove in the Actions pane.
The same can be done via web.config
<configuration>
<system.webServer>
<security>
<requestFiltering>
<hiddenSegments applyToWebDAV="false">
<remove segment="Bin" />
</hiddenSegments>
</requestFiltering>
</security>
</system.webServer>
</configuration>
Anyway, in orded to avoid problems on development server and on any deploy server, i think that the easiest solution is to move that pages to another folder.
Read here:
ASP.NET recognizes certain folder names that you can use
for specific types of content.
The following table lists the reserved folder names and the type
of files that the folders typically contain.
Note
The content of application folders, except for the App_Themes folder,
is not served in response to Web requests,
but it can be accessed from application code.
App_Browsers Contains browser definitions (.browser files) that
ASP.NET uses to identify individual browsers and determine their
capabilities.
App_Code Contains source
code for shared classes and business objects
App_Data Contains application data files
including .mdf database files, XML files, and other data store files.
App_GlobalResources
Contains resources (.resx and .resources files) that are compiled
into assemblies with global scope.
App_LocalResources Contains resources (.resx and
.resources files) that are associated with a specific page, user
control, or master page in an application
App_Themes Contains a collection
of files (.skin and .css files, as well as image files and generic
resources) that define the appearance of ASP.NET Web pages and
controls.
App_WebReferences Contains reference contract files (.wsdl files),
schemas (.xsd files), and discovery document files (.disco and
.discomap files) that let you create a Web reference for use in an
application.
Bin
Contains compiled assemblies (.dll files) for controls, components,
or other code that you want to reference in your application.
Access \bin folder over network share, e.g. \\myserver\mysite\bin
Write ashx handler or asmx web service which will scan \bin folder for you and return the result for you, e.g. Directory.GetFiles(Server.MapPath("~\bin"))

IIS7 Application Configuration

I have a custom ASP.NET application that I utilize for several clients that I host. Each client has a separate domain and the application is normally a child application under the root domain (http://domain.com/customapp). The application files are the same (aspx, ascx, style sheets, images, etc.). The only thing different is the web.config file for each client. As development of the application continues to evolve, I have to update the application for each directory and this obviously becoming tedious. I am trying to come up with a method keep the application up to date. My first though is placing the application into a single physical path and creating multiple applications pointing to that path (the problem with this method is I can't have different web.config files). I am curious as to what solution others are using in this scenario...
If you want to handle this entirely in Visual Studio, VS2010 offers web.config transforms which could solve your problem.
In a nutshell, create a build configuration (In VS, select Build|Configuration Manager...) for each site. Add a web.config transform for each client, which only specifies the differences required for each application.
I use this for differentiating between development, staging and release configurations - each transform adjusts the connection string, app settings, etc - and it works quite well both within Visual Studio and when deploying via MSBuild.
Also, note that web.config settings are inherited by IIS applications. So, if you have a root site
/root
and client apps
/root/client1
/root/client2
...
you could place the client-specific config settings in a web.config in each client-specific folder, and global settings a web.config in the root folder.
Can you just move your web.config content to a database and load it conditionally based on the domain that was referenced?
Select Case Request.Url.Host.ToLowerInvariant()
Case "xyz.com", "www.xyz.com"
'Load XYZ stuff'
Case "abc.com", "www.abc.com"
'Load ABC stuff'
Case Else
'Throw an error probably'
End Select
Even better, store your domains in the database as keys so that you don't ever have to touch the code.

ASP.net externalize settings from web.config

I have web application and and DB connection settings are stored in web.config
The problem is that this application is deployed to multiple servers and connection string may vary.
So I would like to have the same web.config on all server and actual configuration settings be externalized, for example to XXX.properties - this is pretty similar to java world - config file has place holders and values are read from other files
Again: all server has the same web.config files and unique XXX.properties
How can i configure this?
thak you
For connection strings you can use the configSource attribute:
<connectionStrings configSource="connectionStrings.config"></connectionStrings>
At our applications we manage the connection string configuration file varyingly. For some projects we simply store the config file somewhere outside the main project root, so we can safely publish to it. For others with proper CI setup we use a custom build task to copy the correct configuration file depending on the build type.
If you're on Visual Studio 2010, you could also look into Web Config Tranformations.
Just put the location of your external config file in the web.config.
Then in your code, you can read and parse your external file to get the settings for the app.

Do I need a web config file to run an asp.net hello world on IIS 7?

I have uploaded a simple hello world on my IIS server 7 (shared hosting). It doesn't work. Is it necessary to add a web config and what's the minimum in that case ?
Thanks.
Error says:
Server Application Unavailable
The web application you are attempting
to access on this web server is
currently unavailable. Please hit the
"Refresh" button in your web browser
to retry your request.
Here's the script, very basic :)
<%# Page Language="VB" %>
<html>
<head>
<title>ASP.NET Hello World</title>
</head>
<body bgcolor="#FFFFFF">
<p><%= "Hello World!" %></p>
</body>
</html>
A individual, site-specific web.config is not required to be present in order to get a basic "Hello World" site up and running in IIS7, however, it's rather unusual not to have one.
IIS7, unlike previous versions, effectively has the ASP.NET worker process component "built-in". This allows web.config files to specify configuration of not only your ASP.NET site itself, but also how the IIS server hosting your site should be configured (i.e. you can specify (for example) the default document type in an ASP.NET web.config file).
If you don't specify an individual web.config for your ASP.NET site, the IIS7 server will use the "default" web.config, which is usually located in your "windows" folder within the system-wide configuration of the .NET framework itself.
This article:
Working With Configuration Files in IIS 7
from the MSDN library states:
Configuration Files
Configuration exists in a physical
directory in either server-level
configuration files or in Web.config
files. Every configuration file maps
to a specific site, application, or
virtual directory.
Server-level configuration is stored
in the following configuration files:
Machine.config. This file is located in
%windir%\Microsoft.NET\Framework\framework_version\CONFIG.
Root Web.config for the .NET Framework. This file is located in
%windir%\Microsoft.NET\Framework\framework_version\CONFIG.
ApplicationHost.config. This file is located in
%windir%\system32\inetsrv\config.
Site, application, and virtual and
physical directory configuration can
be stored in one of the following
locations:
A server-level configuration file. When configuration for a site,
application, directory, or URL is
stored in a server-level configuration
file, you must use a location tag to
specify the site, application,
directory, or URL to which the
configuration applies.
A parent-level Web.config file. When configuration for an application,
directory, or URL is stored in a
parent-level configuration file, you
must use a location tag to specify the
child at which the configuration
applies.
The Web.config file for the site, the application, or the directory.
When you configure settings for an
application, directory, or URL, the
configuration is stored in the same
directory as the site, application, or
directory. You do not need to use
location tags.
Storing configuration settings in a
parent configuration file is helpful
when:
You want to store configuration settings in a configuration file that
is accessible by only certain users or
groups. For example, the
ApplicationHost.config file is
available only to the Administrator
account and to the members of the
Administrators group on a specific
computer, as well as to domain
administrators when a computer is part
of a domain.
You want to configure a feature at the URL-level (also known as
file-level).
Also, see the following article for further information:
The new Configuration System in IIS 7
EDIT:
Regarding the specific error message that you're getting, I've seen this before on an IIS7 server, and the problem turned out to be the Application Pool that the site was set to use wasn't "running". Going into the IIS7 admin gui and starting the Application Pool cured the problem.
I have also seen this error caused wen the relevant permissions have not been set on the folder containing your website code.
See here, here, and here for further information.
Of course, since you're testing a shared hosting environment, you probably don't have access to the web server itself, and it's difficult to know exactly what you do have access to, administration-wise, through your hosting provider, but they probably have some kind of interface to set permissions on folders/files, so I'd look there first.
Failing that, you may have to include a web.config file in your "test" site as that will allow you to set configurations within IIS7 that you may otherwise have no access to.
Failing that, you may need to speak to your web host's support team.
It should be enough to inherit from machine.config. What's the exception?
I am not sure about the shared Hosting environment. But If you try to create a web application ( try ASP.Net Empty web application template) and just use Response.write("Hello World !") . it will work without any web.config.
I tried my self and it work successfully.
Only problem is that It may ask you if you wanted to debug your application then it requires to add "compilation = true" attribute in web.config.

Resources