Web Api Request.CreateResponse HttpResponseMessage no intellisense VS2012
Solution 1
You need to add a reference to System.Net.Http.Formatting.dll
. The CreateResponse
extension method is defined there.
Solution 2
Because this extension method lives in System.Net.Http
, you just need to include it in your usings statements.
using System.Net.Http;
Solution 3
You can use
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.Gone)
instead of
Request.CreateResponse(HttpStatusCode.Gone)
Solution 4
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
Related videos on Youtube
crizzwald
I write code in many different languages. twitter.com/crizzwald
Updated on February 05, 2020Comments
-
crizzwald over 4 years
For some reason,
Request.CreateResponse
is now "red" in VS2012 and when I hover over the usage the IDE saysCannot resolve symbol 'CreateResponse'
Here is the ApiController Class:
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.Any thoughts?
-
von v. about 11 yearsIt happens a lot to me and the way it comes back is to close down VS and re-open it, annoyance that I have to live with, hope not for long.
-
crizzwald about 11 years@von v. I've tried that, no luck :/
-
StingyJack almost 11 yearsMake sure the file build action property is set to "Compile" and not "Content".
-
-
Linus Caldwell almost 11 yearsThe OP did at line 5. Without it would not compile.
-
Jappie Kerk over 10 yearsThis is actually quite weird, once I put down
this
it said I could remove it... -
cjones26 over 9 yearsiroel, it's probably because your controller is not deriving from ApiController.
-
crthompson about 9 yearsBase class originally was
Controller
in my auto created solution, once I changed it toApiController
suddenly theCreateResponse
was found. -
Lukos almost 9 yearsYou must reference the System.Net.Http.Formatting dll BUT the using is System.Net.Http. You need both.
-
Rudy Scoggins about 6 yearsNote that you will need a Using statement as well after adding the reference. This solved my issue.
-
Majid ALSarra over 5 yearsI think there's something wrong with this implementation. You are creating a response based on default values. Where here: msdn.microsoft.com/en-us/library/hh969014(v=vs.118).aspx it says the generated response from the request will be created "wired up" to the request.. I have no idea what "wired up" means :)
-
SendETHToThisAddress over 2 yearsThanks! This helped me in a project where the implementation is not in a controller.