在vb.net Webapi项目中缺少request.CreateResponse

时间:2013-05-12 16:29:01

标签: asp.net vb.net asp.net-mvc-4 asp.net-web-api

我似乎已经找到了问题,但不知道如何修复它,或者我是否可以。

当我创建新的C# WebAPI项目,并添加System.Net.Http的导入时,我可以访问 Request.CreateResponse<T>

当我创建新的VB.Net WebAPI项目并为System.Net.Http添加导入时,我所拥有的只是Request.createODataErrorResponse

然而,我可以忽略智能感知并访问CreateResponse。似乎VB并没有正确解释各种扩展。

我想修复intellisense,但实际上并不需要它。

谢谢!

史蒂夫

更新 我在2012年8月找到了这个forum post的答案,说明它已被复制,但似乎尚未解决。

我是新来的,应该关闭吗?

1 个答案:

答案 0 :(得分:0)

有一种解决方法

我提出了以下解决方案:

  1. 创建调用原始CreateResponse2方法
  2. 的扩展程序方法CreateResponse
  3. 在您的控制器中,调用新的自定义方法(Request.CreateResponse2(...)
  4. 以下是我的解决方案:

    创建新模块HttpRequestMessageExtensions

     Imports System.Net
     Imports System.Net.Http
     Imports System.Web
     Imports System.Runtime.CompilerServices
     Imports System.Web.Http
    
     Public Module HttpRequestMessageExtensions
    
         <Extension()> _
         Public Function CreateResponse2(Of T)(ByVal request As HttpRequestMessage,     statusCode As HttpStatusCode, value As T) As HttpResponseMessage
             Return request.CreateResponse(Of T)(statusCode, value)
         End Function
    
         <Extension()> _
         Public Function CreateResponse2(Of T)(ByVal request As HttpRequestMessage, statusCode As HttpStatusCode, value As T, Configuration As HttpConfiguration) As HttpResponseMessage
             Return request.CreateResponse(Of T)(statusCode, value, Configuration)
         End Function
    
         <Extension()> _
         Public Function CreateResponse2(Of T)(ByVal request As HttpRequestMessage, statusCode As HttpStatusCode, value As T, mediaType As String) As HttpResponseMessage
             Return request.CreateResponse(Of T)(statusCode, value, mediaType)
         End Function
    
         <Extension()> _
         Public Function CreateResponse2(Of T)(ByVal request As HttpRequestMessage, statusCode As HttpStatusCode, value As T, formatter As Formatting.MediaTypeFormatter) As HttpResponseMessage
             Return request.CreateResponse(Of T)(statusCode, value, formatter)
         End Function
    
         <Extension()> _
         Public Function CreateResponse2(Of T)(ByVal request As HttpRequestMessage, statusCode As HttpStatusCode, value As T, mediaType As Headers.MediaTypeHeaderValue) As HttpResponseMessage
             Return request.CreateResponse(Of T)(statusCode, value, mediaType)
         End Function
    
         <Extension()> _
         Public Function CreateResponse2(Of T)(ByVal request As HttpRequestMessage, statusCode As HttpStatusCode, value As T, formatter As Formatting.MediaTypeFormatter, mediaType As String) As HttpResponseMessage
             Return request.CreateResponse(Of T)(statusCode, value, formatter, mediaType)
         End Function
    
         <Extension()> _
         Public Function CreateResponse2(Of T)(ByVal request As HttpRequestMessage, statusCode As HttpStatusCode, value As T, formatter As Formatting.MediaTypeFormatter, mediaType As Headers.MediaTypeHeaderValue) As HttpResponseMessage
             Return request.CreateResponse(Of T)(statusCode, value, formatter, mediaType)
         End Function
    
     End Module
    

    控制器中的用法:

     Return Request.CreateResponse2(Of SomeClass)(HttpStatusCode.BadRequest, objSomeClass)
    

    <强>截图:

    Working Workaround