2

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

2

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.

  1. To change project type: Right click the Data project -> Property -> Application in the left panel -> Output type -> Class Library

  2. 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.

Shawn Xiao
  • 560
  • 5
  • 18
  • 1
    Thanks Shawn, I see why the App.Config file was created now, it was as you've said the Data project was initially created as a console application. I think this explains why my solution didn't work without the App.Config initially, but I must have changed it to a class library down the line and then saw the App.Config file didn't do anything. I was slightly worried because in the published bin folder, the config file for the Data project was in there with incorrect parameters but it looked like it wasn't being used. Great to get confirmation that this is the case. – intrepidexplorer Apr 29 '20 at 23:20
2

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/

Ajay2707
  • 5,690
  • 6
  • 40
  • 58
  • Thanks Ajay, I've realised now that any new projects get created as a console app initially. You've mentioned about referencing the connection string in the Data project. Is there anything extra that needs to be done or is it just sufficient to reference the Data project on the Web project and VS will automatically reference all the Web.Config details for you? – intrepidexplorer Apr 29 '20 at 23:25
  • sorry, I forgot, you can add connection string in your data project and while giving data project reference in your main project, it also gets connection string in your main project. So there is a single connection string which in your data project and give reference of data project where you needed. – Ajay2707 Apr 30 '20 at 04:22