在ASP.Net 5中,如果我正在创建ASP.Net中间件,如何检索请求的物理路径?
in
以前我会使用杠杆:
HttpServerUtility.MapPath()
或
System.Web.Request.PhysicalPath()
新的public async Task Invoke(HttpContext context)
{
var request = context.Request
对象只有Microsoft.AspNet.Http.Request
属性,我不清楚如何将请求路径转换为文件系统路径。
答案 0 :(得分:1)
您可以按如下方式使用IHostingEnvironment服务:
public class PhysicalPathMiddleware
{
private readonly RequestDelegate _next;
private readonly IHostingEnvironment _hostingEnvironment;
public PhysicalPathMiddleware(RequestDelegate next, IHostingEnvironment hostingEnvironment)
{
_next = next;
_hostingEnvironment = hostingEnvironment;
}
public async Task Invoke(HttpContext context)
{
var physicalFileInfo = _hostingEnvironment.WebRootFileProvider.GetFileInfo(context.Request.Path);
var physicalFilePath = physicalFileInfo.PhysicalPath;
}
}