For some reason, Request.CreateResponse
is now "red" in VS2012 and when I hover over the usage the IDE says
Cannot 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?
You need to add a reference to System.Net.Http.Formatting.dll
. The CreateResponse
extension method is defined there.
Because this extension method lives in System.Net.Http
, you just need to include it in your usings statements.
using System.Net.Http;
You can use
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.Gone)
instead of
Request.CreateResponse(HttpStatusCode.Gone)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With