Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC and two Web.config files

Where is the Web.config supposed to go in an ASP.NET MVC project?

I just ran into an error trying to do this:

_cnstr = System.Configuration.ConfigurationManager.     ConnectionStrings["production"].ConnectionString; 

The default MVC template puts the Web.config at the root of the project.

If you go into the properties of a project (the screen with the vertical tabs). Go to settings and try to create an application setting, it will prompt you that you don't have a config file. When it creates the file it does it at the base of the Views folder. So now I have two Web.config files. Is this how it supposed to be?

And I guess I should put my connection string in the "views" web.config to avoid the error. Thoughts? Is this a bug in the last release of the ASP.NET MVC bits?

UPDATE: See David's answer

like image 601
BuddyJoe Avatar asked Feb 05 '09 18:02

BuddyJoe


People also ask

Why are there two web config files in MVC?

So logically if the view folder and view (webpages) are accessible or can be requested by user then the basic MVC pattern and practice followed by Microsoft will be violated. This is prevented by adding a web. config and blocking access to view folder and files directly via user request or through URL.

Can we have multiple Web config files in MVC?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration.

Can you have multiple Web config files?

There is no restriction to use the web. config file in the asp.net web application. You can have 1 Web. config file per folder .


1 Answers

The settings should go into the web.config at the application root. The web.config in the views folder is there to block direct access to the view aspx pages which should only get served through controllers.

(And: I tried creating application settings on my machine, with ASP.NET MVC RC 1 installed, using a newly created mvc web application. They get added to the web.config at the application root.)

like image 156
David Tischler Avatar answered Oct 10 '22 21:10

David Tischler