0

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

_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

当我通过 Visual Studio 启动应用程序时,它工作正常,它会找到图像。但是当我部署到 AWS 或本地 IIS 时,我收到以下错误:

DirectoryNotFoundException: Could not find a part of the path 'C:\inetpub\wwwroot\MyApp\Resources\imgs\quote_background.png'.

引用此图像的正确方法是什么?

谢谢

4

2 回答 2

2

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

属性屏幕截图显示复制到输出直接设置为复制(如果较新)

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

于 2017-07-16T15:47:10.490 回答
1

您需要使用 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。

于 2017-07-16T05:34:21.770 回答