【发布时间】:2019-01-10 05:51:54
【问题描述】:
在我的应用程序中,我将一些图像文件保存到应用程序文件夹本身。当前使用IHostingEnvironment 接口获取路径。
喜欢
private readonly IHostingEnvironment _hostingEnvironment;
/// <summary>
/// Initializes a new instance of the <see cref="ProductController"/> class.
/// </summary>
/// <param name="unitService">The product service.</param>
public ProductController(IProductService productService, IHostingEnvironment hostingEnvironment)
{
this._productService = productService;
this._hostingEnvironment = hostingEnvironment;
}
使用此代码获取路径_hostingEnvironment.ContentRootPath
但是以后我们可能会将图像位置更改为云端或其他一些地方,所以我写了一个扩展方法来获取实际路径
public static class AssetPathHandlerExtensions
{
private readonly IHostingEnvironment _hostingEnvironment;//error
public static string AppendAssetPath(this string fileName, string subDirectryPath)
{
//here i need to get the ContentRootPath and append it with filename
}
}
这个扩展方法在类库中,我从automapper Mapping 调用这个扩展方法。
我面临的问题是我不能在类库中使用IHostingEnvironment,因为它不包含Microsoft.AspNetCore.Hosting.Abstractions.dll 程序集。
有没有办法在类库中使用IHostingEnvironment?
【问题讨论】:
标签: asp.net-core asp.net-core-mvc asp.net-core-2.1