1

我对 Server.MapPath 方法有疑问。

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

如果我尝试在返回 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
4

1 回答 1

1

传递给回调函数的第二个参数是 type HttpContent。你有效地隐藏HttpContext在你的 lambda 中,因为你被命名为 parameter 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函数定义为:

Public Sub New ( _
    onStreamAvailable As Action(Of Stream, HttpContent, TransportContext) _
)
于 2014-03-03T08:23:16.717 回答