2014-03-03 60 views
1

我有方法的使用Server.Mappath发行虚拟路径。映射到物理路径

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

如果我尝试用同样的方法在返回HttpResponse的ApiController,它给人的错误:

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

我怎样才能使用的方法Server.MapPathHttpResponseMessage的背景下?

(我在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 
+2

有没有可能是你输错/智能感知帮助你键入'HttpContent'当你想用'HttpContext'? –

+0

我附上了我使用的确切代码。我正在使用HttpContext。但是,我也在函数中使用HttpContext,也许这就是问题所在? – user2399378

+0

我已经把你的编辑回来了。这对于人们看到实际的代码很有用。另外,如果没有问题中的代码,它会让我看起来很精神,这是没有根据的(在这种情况下)确切地知道错误在代码中的位置。 –

回答

1

传递给你的回调函数的第二个参数是HttpContent类型。你实际上隐藏了HttpContext,因为你被命名为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) _ 
) 
+0

是的,这是问题,我只注意到它,我解决它通过使用System.Web.HttpContext,但您的解决方案是更好的选择。谢谢 – user2399378