将虚拟路径映射到物理路径

时间:2014-03-03 08:08:27

标签: vb.net server.mappath

我遇到了Server.MapPath方法的问题。

我目前正在使用方法HttpContext.Current.Server.MapPath("MyPath")在ApiController,get方法中获取“MyPath”的物理路径。

如果我尝试在返回HttpResponse的ApiController中使用相同的方法,则会出现错误:

  

Current不是System.Net.Http.HttpContent

的成员

如何在Server.MapPath的上下文中使用方法HttpResponseMessage

(我在Visual Basic中工作)

编辑:我这样使用它:

<HttpGet>
Public Function MyFunc() As HttpResponseMessage
    Try
        Dim streamContent = New PushStreamContent(_
                       Function(outputStream, httpContext, transportContent)
         Try
Dim lPath = httpContext.Current.Server.MapPath(MyPath)

        .... some code

                End Try
    End Function)
 Dim lResult = New HttpResponseMessage(HttpStatusCode.OK)
 lResult.Content = streamContent

 Return lResult
    End Try
   End Function

1 个答案:

答案 0 :(得分:1)

传递给回调函数的第二个参数是HttpContent类型。您有效地将HttpContext隐藏在lambda中,因为您将该参数命名为HttpContext。尝试:

Dim streamContent = New PushStreamContent(_
    Function(outputStream, content, transportContent) 'Renamed parameter here
         Try
             Dim lPath = HttpContext.Current.Server.MapPath(MyPath)

         '.... some code
         Catch ex As Exception
         Finally
             outputStream.Close()
         End Try
    End Function)

PushStreamContent constructor定义是:

Public Sub New ( _
    onStreamAvailable As Action(Of Stream, HttpContent, TransportContext) _
)