问题描述
我似乎已经找到了问题,但不知道如何解决它,或者如果我能。
I seem to have tracked down the problem but do not know how to fix it, or if I can.
当我创建一个新的 C#的WebAPI
项目,并添加一个导入为 System.Net.Http
我可以访问 Request.CreateResponse< T>
When I create a new C# WebAPI
project, and add an import for System.Net.Http
I can accessRequest.CreateResponse<T>
.
当我创建一个新的 VB.Net的WebAPI
项目,并添加一个导入为 System.Net.Http
我只有 Request.createODataErrorResponse
。
When I create a new VB.Net WebAPI
project, and add an import for System.Net.Http
all I have is Request.createODataErrorResponse
.
我可以忽略但智能感知和接入 CreateResponse
反正。 VB似乎仅仅是不跨$ P $正确pting各种扩展。
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.
谢谢!
史蒂夫
更新
我发现这个一个答案早在2012年8月解释说,它已重复的,但还没有出现被固定
UpdateI 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?
推荐答案
有一种方法
我想出了以下解决方案:
I came up with the following solution:
- 创建扩展方法
CreateResponse2
调用原始CreateResponse
法 - 在你的控制器,调用新的自定义方法(
Request.CreateResponse2(...)
)
- Create extensions methods
CreateResponse2
that call the originalCreateResponse
method - In your controllers, call the new custom method (
Request.CreateResponse2(...)
)
下面是我的解决办法:
创建一个新模块的Htt prequestMessageExtensions
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)
截图:
这篇关于缺少request.CreateResponse在vb.net的WebAPI项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!