C# Web Api Request.CreateResponse HttpResponseMessage 没有智能感知 VS2012
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15816049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Web Api Request.CreateResponse HttpResponseMessage no intellisense VS2012
提问by crizzwald
For some reason, Request.CreateResponse
is now "red" in VS2012 and when I hover over the usage the IDE says
出于某种原因,Request.CreateResponse
现在在 VS2012 中为“红色”,当我将鼠标悬停在使用上时,IDE 说
Cannot resolve symbol 'CreateResponse'
无法解析符号“CreateResponse”
Here is the ApiController Class:
这是 ApiController 类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Filters;
using GOCApi.Attributes;
using GOCApi.Models;
using GOCApi.Models.Abstract;
using AttributeRouting;
using AttributeRouting.Web.Http;
namespace GOCApi.Controllers
{
[RoutePrefix("Courses")]
public class CoursesController : ApiController
{
private ICoursesRepository _coursesRepository { get; set; }
public CoursesController(ICoursesRepository coursesRepository)
{
_coursesRepository = coursesRepository;
}
[GET("{id}")]
public HttpResponseMessage Get(int id)
{
var course = _coursesRepository.GetSingle(id);
if (course == null)
return Request.CreateResponse(HttpStatusCode.NotFound, "Invalid ID");
return Request.CreateResponse(HttpStatusCode.OK, course);
}
}
}
Granted, the code does compile and works, it's just really annoying seeing all the "red" on my screen. Also, the intellisense doesn't work now when I type Request.CreateResponse
. This also used to work, but I started developing other parts to my API and just came back to building controllers so I do not know what happened.
当然,代码确实可以编译并运行,但看到屏幕上所有的“红色”真的很烦人。此外,当我输入Request.CreateResponse
. 这也曾经有效,但我开始为我的 API 开发其他部分,然后又回到构建控制器,所以我不知道发生了什么。
Any thoughts?
有什么想法吗?
回答by scidec
Because this extension method lives in System.Net.Http
, you just need to include it in your usings statements.
由于此扩展方法存在于 中System.Net.Http
,因此您只需要将其包含在 using 语句中。
using System.Net.Http;
回答by Rafael Rom?o
You need to add a reference to System.Net.Http.Formatting.dll
. The CreateResponse
extension method is defined there.
您需要添加对System.Net.Http.Formatting.dll
. 该CreateResponse
扩展方法被定义在那里。
回答by Simcha Khabinsky
This a known issue with VB.NET and Request.CreateResponse.
这是 VB.NET 和 Request.CreateResponse 的已知问题。
There is a workaround: Missing request.CreateResponse in vb.net Webapi Projects
回答by Jappie Kerk
This is because you have to do:
这是因为你必须这样做:
namespace GOCApi.Controllers
{
[RoutePrefix("Courses")]
public class CoursesController : ApiController
{
private ICoursesRepository _coursesRepository { get; set; }
public CoursesController(ICoursesRepository coursesRepository)
{
_coursesRepository = coursesRepository;
}
[GET("{id}")]
public HttpResponseMessage Get(int id)
{
var course = _coursesRepository.GetSingle(id);
if (course == null){
return this.Request.CreateResponse(HttpStatusCode.NotFound, "Invalid ID");
}
return this.Request.CreateResponse(HttpStatusCode.OK, course);
}
}
}
Note the this
.
In my case the compiler now gets it.
Saw the example here
请注意this
.
在我的情况下,编译器现在得到了它。看到这里的例子
回答by Diwakar Sharma
You can use
您可以使用
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.Gone)
instead of
代替
Request.CreateResponse(HttpStatusCode.Gone)
回答by mmcc
Add a reference to System.Web.Http
to the project. Then add
添加System.Web.Http
对项目的引用。然后加
Using System.Web
to the top of the cs file.
到 cs 文件的顶部。