ASP.NET Core 3.1 WebRoot 路径 [英] ASP.NET Core 3.1 WebRoot Path

查看:42
本文介绍了ASP.NET Core 3.1 WebRoot 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个外部文件推送到我的 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.

如果你想改变这种行为,你必须在构建网络主机时调用这一行.

If you want to change that behavior, you have to call this line when building the web-host.

.UseContentRoot(AppContext.BaseDirectory);

然后路径将更改为 Debug etcoreapp3.1 或您正在编译的任何内容.

Then the path will change to <ProjectPath>Debug etcoreapp3.1 or whatever you are compiling to.

这篇关于ASP.NET Core 3.1 WebRoot 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆