Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 发布时未找到.Net Core引用的文件_File_Asp.net Core_Imagesharp - Fatal编程技术网

File 发布时未找到.Net Core引用的文件

File 发布时未找到.Net Core引用的文件,file,asp.net-core,imagesharp,File,Asp.net Core,Imagesharp,我正在使用ImageSharp和.NETCore来处理一些图像。要加载图像和字体,我执行以下操作: _image = Image.Load(@"Resources/imgs/quote_background.png"); _fonts = new FontCollection(); _font = _fonts.Install(@"Resources/fonts/Cousine-Italic.ttf"); // Image processing... 我的文件树如下所示: - So

我正在使用ImageSharp和.NETCore来处理一些图像。要加载图像和字体,我执行以下操作:

_image = Image.Load(@"Resources/imgs/quote_background.png");

_fonts = new FontCollection();
_font = _fonts.Install(@"Resources/fonts/Cousine-Italic.ttf");

// Image processing...
我的文件树如下所示:

    - Solution
    - - MyApp
    - - - Controllers
    - - - Models
    - - - - Code.cs // This is where the above code is
    - - - wwwroot
    - - - Resources
    - - - - imgs
    - - - - fonts
当我通过VisualStudio启动应用程序时,它工作正常,可以找到图像。但当我部署到AWS或本地IIS时,会出现以下错误:

DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\wwwroot\MyApp\Resources\imgs\quote_background.png'.
引用此图像的正确方式是什么


谢谢

您需要使用IHostingEnvironment中的ContentRootPath,这要求您将IHostingEnvironment注入控制器,例如:

public class ImageController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public ImageController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public ActionResult Index()
    {
        var image = Image.Load(String.Format(@"{0}/Resources/imgs/quote_background.png", 
            _hostingEnvironment.ContentRootPath);
        //etc...
    }
}

还有一个WebRootPath可以让您访问wwwroot。

您需要使用IHostingEnvironment中的ContentRootPath,这要求您将IHostingEnvironment注入控制器,例如:

public class ImageController : Controller
{
    private readonly IHostingEnvironment _hostingEnvironment;

    public ImageController(IHostingEnvironment hostingEnvironment)
    {
        _hostingEnvironment = hostingEnvironment;
    }

    public ActionResult Index()
    {
        var image = Image.Load(String.Format(@"{0}/Resources/imgs/quote_background.png", 
            _hostingEnvironment.ContentRootPath);
        //etc...
    }
}

还有一个WebRootPath,可以让您访问wwwroot。

您想确保将
资源
文件夹中的文件标记为“复制到输出目录”=“复制到更新的目录”


这将确保在发布网站时,文件最终会出现在您的输出中。

您要确保将
资源
文件夹中的文件标记为“复制到输出目录”=“如果更新,则复制”


这将确保在您发布网站时,文件最终会出现在您的输出中。

嗨,Matt,谢谢您的回答。我照你说的做了,但还是犯了同样的错误。它似乎正在映射到与以前相同的目录。。。当我发布项目时,资源文件夹确实没有生成。。。你认为我必须配置一些东西吗…?好的,看看另一个答案,你的图片真的被发布了吗?谢谢Matt。。。虽然我不得不手动编辑我的.csproj,但它确实解决了问题,因为当我试图设置属性时,visual studio提示了一个错误…嗨,Matt,谢谢你的回答。我照你说的做了,但还是犯了同样的错误。它似乎正在映射到与以前相同的目录。。。当我发布项目时,资源文件夹确实没有生成。。。你认为我必须配置一些东西吗…?好的,看看另一个答案,你的图片真的被发布了吗?谢谢Matt。。。虽然我不得不手动编辑我的.csproj,但它确实解决了问题,因为当我试图设置属性时,visual studio提示了一个错误。。。