在我的应用程序中,我将一些图像文件保存到应用程序文件夹中。目前使用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
映射中调用该扩展方法。
我遇到的问题是,我不能在类库中使用IHostingEnvironment
,因为它不包含Microsoft.AspNetCore.Hosting.Abstractions.dll
程序集。
有没有办法在类库中使用IHostingEnvironment
?