我使用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'.
参考此图片的正确方法是什么?
由于
答案 0 :(得分: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。
答案 1 :(得分:1)