ASP.NET Core 3.1 WebRoot路径 [英] ASP.NET Core 3.1 WebRoot Path
问题描述
我正在将外部文件推送到ASP.NET Core 3.1应用程序的wwwroot文件夹中.在服务连接期间,我需要引用路径以提供构造函数变量.
I am pushing an external file into the wwwroot folder of my ASP.NET Core 3.1 application. I need to reference the path to provide a constructor variable during the services wire up.
在Startup类中,我已将IWebHostEnvironment添加到构造函数参数中:
In the Startup class I have added the IWebHostEnvironment to the constructor parameters:
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
_env = env;
}
调试时,似乎 _env.WebRootPath
返回源文件夹中的路径,而不是正在执行的路径,即
When debugging, it seems that _env.WebRootPath
returns the path as it is in my source folder, rather than the path being executed, i.e.
它返回:<我的源代码的路径>/wwwroot
代替执行相对路径:<我的源代码的路径>/bin/Debug/netcoreapp3.0/wwwroot
我如何获取它返回正确的,执行相对路径?
How do i get it to return the correct, execution relative path?
推荐答案
这是因为默认情况下,内容根目录路径是项目所在的目录,而不是项目的输出.
It's because by default the content-root path is the directory where the project is located rather than its output.
如果要更改此行为,则在构建Web主机时必须调用此行.
If you want to change that behavior, you have to call this line when building the web-host.
.UseContentRoot(AppContext.BaseDirectory);
然后路径将更改为< ProjectPath> \ Debug \ netcoreapp3.1
或要编译的任何内容.
Then the path will change to <ProjectPath>\Debug\netcoreapp3.1
or whatever you are compiling to.
这篇关于ASP.NET Core 3.1 WebRoot路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!