我遇到了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
答案 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) _
)