Missing request.CreateResponse in vb.net Webapi Projects

前端 未结 1 1077
再見小時候
再見小時候 2021-01-18 06:14

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 S

相关标签:
1条回答
  • 2021-01-18 06:30

    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:

    Working Workaround

    0 讨论(0)
提交回复
热议问题