问题
There are a bunch of contradictory statements about web.config and .NET Core.
- Places saying it's gone.
- https://dotnetcore.show/episode-10-configuration-in-net-core/
- Places saying it was gone but now it's back.
- https://stackoverflow.com/a/46771737/1662268
- Places saying that it's no longer necessary, but you can create one manually:
- https://stackoverflow.com/a/43890405/1662268
- https://stackoverflow.com/a/53731666/1662268
Additionally, I've just deployed a mostly-fresh-out-of-the-box .NET Core app to Azure. In the repository, there's no web.config but when I explore the deployed code in Kudu, there IS a web.config file?
What's going on with web.config in .NET Core? How are you supposed to do non-Custom-App-Configuration stuff? (e.g. HTTP headers and other HTTP level config)
回答1:
In a .NET Framework Web application, web.config is used for two purposes:
To store configuration for the web application.
To store configuration for the IIS that hosts the application (
system.webServer
element).
In a .NET Core Web application, you have at least two choices for storing application configuration:
Use a .NET Core Configuration Provider, often with a json configuration file.
Use the System.Configuration.ConfigurationManager NuGet package with recent versions of .NET Core (2.0+ I think). One advantage of using
System.Configuration.ConfigurationManager
is that it can be used in .NET Standard 2.0 libraries that are compatible with both .NET Core and .NET Framework.
If you choose to use System.Configuration.ConfigurationManager
in a .NET Core web application, you need to add an app.config
(not web.config
) file to your Visual Studio project. When you build this project it will be copied to the output directory and renamed as appName.dll.config
.
If you are hosting your .NET Core web application in IIS, you can still need a web.config
file with configuration for IIS (system.webServer
element). I.e. your application may need both app.Config
and web.config
.
Personally I prefer to use System.Configuration.ConfigurationManager
and an app.config
XML configuration file, for several reasons including compatibility with .NET Framework, the fact that it's easy to comment out bits of XML, etc. YMMV of course.
回答2:
Read a few web.config
examples from Google and you should notice that,
- ASP.NET/WCF 2.x/4.x uses it to store configuration (tags like
<system.web>
). - IIS uses it to store configuration (tags like
<system.webServer>
).
So migrating to ASP.NET Core only make the first obsolete, and you should only say goodbye to that piece of knowledge.
If you host your web app on IIS, then web.config
is still needed, as you need initial settings there to configure ASP.NET Core module for IIS/IIS Express (dotnet publish
generates such a file if you don't have your own).
回答3:
This may be incorrect feel free to correct me but the way I understand it.
web.config was used to give some information to the hosting software like IIS some configuration details to help run the app correctly.
In dotnet core You have a layer between the hosting software like IIS and your Web App, and it's called Kestrel.
Kestrel is a proxy between the two. And the default web.config that you see, if you peek inside you will see default code which basically says, i'm a dotnet core app use the dotnet core runtime to run me. You can still put config there to tell IIS how to work, I specifically used it to increase the file upload size and windows authentication.
I would configure HTTP Headers using the middle ware.
来源:https://stackoverflow.com/questions/54158609/am-confused-about-web-config-in-net-core