【问题标题】:Map Virtual path to physical path将虚拟路径映射到物理路径
【发布时间】:2014-03-03 08:08:26
【问题描述】:

我对方法 Server.MapPath 有疑问。

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

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

Current 不是System.Net.Http.HttpContent 的成员

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

(我正在使用 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

【问题讨论】:

  • 有没有可能是你刚才打错了/intellisense 在你想输入HttpContext 时帮助你输入了HttpContent
  • 我附上了我使用的确切代码。我正在使用 HttpContext。但是,我在那里的函数中也使用了HttpContext,也许这是问题所在?
  • 我已撤回您的编辑。人们查看实际代码很有用。此外,如果没有问题中的代码,它会让我显得通灵,(在这种情况下)没有理由确切地知道代码中的错误所在。

标签: vb.net server.mappath


【解决方案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) _
)

【讨论】:

  • 是的,这就是问题所在,我刚刚注意到它,我通过使用 System.Web.HttpContext 解决了它,但是您的解决方案是一个更好的解决方案。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-30
  • 2011-08-30
  • 2011-06-29
相关资源
最近更新 更多