Web Api Request.CreateResponse HttpResponseMessage no intellisense VS2012

75,511

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

Share:
75,511

Related videos on Youtube

crizzwald
Author by

crizzwald

I write code in many different languages. twitter.com/crizzwald

Updated on February 05, 2020

Comments

  • crizzwald
    crizzwald over 4 years

    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?

    • von v.
      von v. about 11 years
      It 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
      crizzwald about 11 years
      @von v. I've tried that, no luck :/
    • StingyJack
      StingyJack almost 11 years
      Make sure the file build action property is set to "Compile" and not "Content".
  • Linus Caldwell
    Linus Caldwell almost 11 years
    The OP did at line 5. Without it would not compile.
  • Jappie Kerk
    Jappie Kerk over 10 years
    This is actually quite weird, once I put down this it said I could remove it...
  • cjones26
    cjones26 over 9 years
    iroel, it's probably because your controller is not deriving from ApiController.
  • crthompson
    crthompson about 9 years
    Base class originally was Controller in my auto created solution, once I changed it to ApiController suddenly the CreateResponse was found.
  • Lukos
    Lukos almost 9 years
    You must reference the System.Net.Http.Formatting dll BUT the using is System.Net.Http. You need both.
  • Rudy Scoggins
    Rudy Scoggins about 6 years
    Note that you will need a Using statement as well after adding the reference. This solved my issue.
  • Majid ALSarra
    Majid ALSarra over 5 years
    I 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
    SendETHToThisAddress over 2 years
    Thanks! This helped me in a project where the implementation is not in a controller.