Missing request.CreateResponse in vb.net Webapi Pr

2020-04-07 03:22发布

问题:

I seem to have tracked down the problem but do not know how to fix it, or if I can.

When I create a new C# WebAPI project, and add an import for System.Net.Http I can access Request.CreateResponse<T>.

When I create a new VB.Net WebAPI project, and add an import for System.Net.Http all I have is Request.createODataErrorResponse.

I can however ignore intellisense and access CreateResponse anyway. Seems VB just isn't interpreting the various extensions correctly.

I'd like to fix the intellisense but don't really have a need for it.

Thanks!

Steve

Update I found this forum post with an answer back in August 2012 explaining that it had been duplicated, but does not yet appear to be fixed.

As I am new here, should this be closed?

回答1:

There is a workaround

I came up with the following solution:

  1. Create extensions methods CreateResponse2 that call the original CreateResponse method
  2. In your controllers, call the new custom method (Request.CreateResponse2(...))

Below is my solution:

Create a new module 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

Usage in controllers:

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

Screenshot: