how to reproduce this problem:
如何重现此问题:
- use .net 7 sdk to create an asp.net core webapi project
- modify .csproj file to same with below:
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<DebugType>embedded</DebugType>
</PropertyGroup>
use this command to publish: dotnet publish -r win-x64
you can find these files in publish dir:
- appsettings.Development.json
- appsettings.json
- DemoProject.exe (i only want this file)
- web.config
is there any way to include appsettings.json
,appsettings.Development.json
and web.config
file?
有没有办法包含appsettings.json、appsettings.Development.json和web.config文件?
更多回答
The point of these config/settings files is that you can edit them. Including them in the executable would make this very difficult. Have you tried just deleting them and testing again? Maybe they are optional.
这些配置/设置文件的意义在于您可以对其进行编辑。将它们包含在可执行文件中会使这一点变得非常困难。你有没有试过删除它们,然后重新测试?或许它们是可选的。
@Lynn as far as I know,springboot (a java framework) will package settings file into jar file, and you can override some settings by create a new settings file around jar file , it seems a little hard to implement this behavior in aspnetcore
@Lynn据我所知,SpringBoot(一个Java框架)会将设置文件打包到JAR文件中,您可以通过在JAR文件周围创建一个新的设置文件来覆盖某些设置,在aspnetcore中实现这一行为似乎有些困难
If you are selecting windows as the target, it will generate the web.config which is used for IIS hosting. If you select osx or linux it will not generate the web.config file. But appsetting.json will always generate by default. If you don't want it, you could delete it and write all the settings inside the program.cs. \
如果您选择Windows作为目标,它将生成用于IIS托管的web.config。如果您选择OSX或Linux,它将不会生成web.config文件。但默认情况下,始终会生成appsetting.json。如果你不想要它,你可以把它删除,然后在Program.cs中写下所有的设置。\
优秀答案推荐
<SelfContained>true</SelfContained>
will not purely create a single exe.
True
不会纯粹创建单个EXE。
Instead, It will create a self-extracting exe with an embedded zip file which will extract the all required files to a temporary path and then start executing the exe.
相反,它将创建一个带有嵌入的压缩文件的自解压exe,该文件将解压所有需要的文件到临时路径,然后开始执行该exe。
The structure of appsettings.json
and web.config
are based on the Microsoft's recommended best practices to run ASP.NET code for easy maintenance. You can always remove this if you want.
为便于维护,appsettings.json和web.config的结构基于微软推荐的运行ASP.NET代码的最佳实践。如果您愿意,您可以随时删除它。
If you use Kestrel instead of IIS, you are free to remove the web.config
file.
如果您使用Kestrel而不是IIS,则可以自由删除web.config文件。
Additionally use can get rid of appsettings file if you override the default behaviour by hard coding the default setting inside the code.
此外,如果您通过在代码中对默认设置进行硬编码来覆盖默认行为,则Use可以删除appsets文件。
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/webapplication?view=aspnetcore-7.0
Https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/webapplication?view=aspnetcore-7.0
更多回答
我是一名优秀的程序员,十分优秀!