有一个解决方法
我想出了以下解决方案:
CreateResponse2
创建调用原始CreateResponse
方法的扩展方法
- 在您的控制器中,调用新的自定义方法 (
Request.CreateResponse2(...)
)
以下是我的解决方案:
创建一个新模块 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)
截屏: