Reputation: 75
When creating an ASP.NET MVC project, a web.config
file gets generated automatically and is used for all the connection strings and dll versions relevant to the solution.
I've added a new project to the solution for handling data operations using Entity Framework and can see that it has it's own app.config
that gets created as well.
Previously, all data and web was on the one ASP.NET project and now I've split them up into two different projects so I can have the web project just have web components and data project have database connections using Entity Framework.
Initially it looked like the app.config
of the data project would require the connection strings to the database be present as well, but it looks like the data project can connect fine to the database using the connection strings from the web.config
file.
I've tried removing the app.config
file completely from the data project and it looks like the solution runs even without it.
Are there any special reasons the app.config
file exists in an ASP.NET MVC project or can this file be safely deleted without affecting the website?
For example, I tried putting in 2 different connection strings, one in web.config
and a different one in app.config
. When accessing the site, it looks like only the connection strings and app variables from the web.config
file is used.
Is there some sort of hierarchy that gets used where the app.config
details will override the web.config
details?
Upvotes: 2
Views: 2729
Reputation: 5808
Both are used to configure the project, the only difference is the type of project.
From the source
Web.Config
is used for asp.net web projects / web services. (or Say Web application)
App.Config
is used for Windows Forms, Windows Services, Console Apps and WPF applications (or say window application)
So might be your second project for data is window or console-based, that's why created.
Until any file in a project, you do not have the reference in other files, you can remove and run the project.
Ideally connection string at your main project and give the reference in the data project which is handling the single connection string in the solution or in multiple projects.
https://www.c-sharpcorner.com/UploadFile/8a67c0/web-config-vs-app-config-vs-machine-config/
Upvotes: 2
Reputation: 590
In .Net, there only have three types of project: windows application, console application and class library.
The app.config will be generated automatically with the first two type applications, and it will be used while the application running;
For the web site/application/services, the web.config will be generated and used while accessing web pages. But actually, the web site/application/services are class library type.
In your case, the Data project might be created as a console/windows application, so the app.config will be generated. So you should change the Data project to class library and add a project reference in the Web project, then you can delete app.config safely.
To change project type: Right click the Data project -> Property -> Application in the left panel -> Output type -> Class Library
To add project reference: Right click the Web project -> Add -> Reference.. -> Projects in the left panel -> check the Data project -> OK
Finial, the reason why only the connection string in web.config is being used is: the Data project is treated as a class library and it will fetch the configurations from web.config at the runtime. And if you run the Data project alone, it will fetch the configurations in app.config instead.
Upvotes: 2