Question

I have an issue with the method Server.MapPath.

I am currently using the method HttpContext.Current.Server.MapPath("MyPath") to obtain the physical path of "MyPath" in an ApiController, get method.

If I try to use the same method in an ApiController which returns an HttpResponse, it gives the error:

Current is not a member of System.Net.Http.HttpContent

How can I use the method Server.MapPath in the context of an HttpResponseMessage?

(I am working in Visual Basic)

Edit: I am using it like this:

<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
Was it helpful?

Solution

The second argument passed to your callback function is of type HttpContent. You're effectively hiding HttpContext inside your lambda because you're named that parameter HttpContext. Try:

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)

The PushStreamContent constructor definition is:

Public Sub New ( _
    onStreamAvailable As Action(Of Stream, HttpContent, TransportContext) _
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top