27

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?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
crizzwald
  • 1,037
  • 2
  • 14
  • 30

6 Answers6

22

You need to add a reference to System.Net.Http.Formatting.dll. The CreateResponse extension method is defined there.

Rafael Romão
  • 1,788
  • 3
  • 20
  • 35
21

Because this extension method lives in System.Net.Http, you just need to include it in your usings statements.

using System.Net.Http;
Linus Caldwell
  • 10,908
  • 12
  • 46
  • 58
scidec
  • 247
  • 2
  • 4
9

You can use

 HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.Gone)

instead of

 Request.CreateResponse(HttpStatusCode.Gone)
Ashkan S
  • 10,464
  • 6
  • 51
  • 80
Diwakar Sharma
  • 101
  • 1
  • 2
  • 1
    I think there's something wrong with this implementation. You are creating a response based on default values. Where here: https://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 :) – Majid ALSarra Aug 19 '18 at 08:03
  • Thanks! This helped me in a project where the implementation is not in a controller. – SendETHToThisAddress Aug 18 '21 at 23:05
6

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

Community
  • 1
  • 1
Jappie Kerk
  • 1,257
  • 12
  • 16
  • This is actually quite weird, once I put down `this` it said I could remove it... – Jappie Kerk Jan 19 '14 at 13:28
  • 2
    Base class originally was `Controller` in my auto created solution, once I changed it to `ApiController` suddenly the `CreateResponse` was found. – crthompson May 14 '15 at 21:23
0

Add a reference to System.Web.Http to the project. Then add

Using System.Web 

to the top of the cs file.

MeanGreen
  • 3,098
  • 5
  • 37
  • 63
mmcc
  • 1
-2

This a known issue with VB.NET and Request.CreateResponse.

There is a workaround: Missing request.CreateResponse in vb.net Webapi Projects

Community
  • 1
  • 1
Simcha Khabinsky
  • 1,970
  • 2
  • 19
  • 34