查找请求的物理路径

时间:2015-09-29 23:41:19

标签: asp.net asp.net-core

在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属性,我不清楚如何将请求路径转换为文件系统路径。

1 个答案:

答案 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;
    }
}