Aru*_*K V 3 asp.net-core-mvc asp.net-core asp.net-core-2.1
在我的应用程序中,我将一些图像文件保存到应用程序文件夹本身中。当前使用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;
}
Run Code Online (Sandbox Code Playgroud)
使用此代码获取路径 _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
}
}
Run Code Online (Sandbox Code Playgroud)
这个扩展方法在一个类库中,我从automapperMapping调用这个扩展方法。
我面临的问题是我不能IHostingEnvironment 在类库中使用 ,因为它不包含Microsoft.AspNetCore.Hosting.Abstractions.dll程序集。
有什么办法可以IHostingEnvironment 在类库中使用吗?
在你的类库中使用IHostingEnvironment没有问题。只需在您的类库中使用此命令安装其 NuGet 包:
Install-Package Microsoft.AspNetCore.Hosting
Run Code Online (Sandbox Code Playgroud)
并像这样从 DI 容器解析IHostingEnviroment:
public class SomeClass
{
private IHostingEnvironment _hostingEnvironment;
public SomeClass(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public void SomeMethod()
{
// Use IHostingEnvironment with _hostingEnvironment here.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1966 次 |
| 最近记录: |