Web Api Request.CreateResponse HttpResponseMessage no intellisense VS2012

前端 未结 6 1989
渐次进展
渐次进展 2020-12-08 19:42

For some reason, Request.CreateResponse is now \"red\" in VS2012 and when I hover over the usage the IDE says

Cannot resolve symbol \'Cre

相关标签:
6条回答
  • 2020-12-08 20:21

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

    using System.Net.Http;
    
    0 讨论(0)
  • 2020-12-08 20:25

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

    0 讨论(0)
  • 2020-12-08 20:25

    You can use

     HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.Gone)
    

    instead of

     Request.CreateResponse(HttpStatusCode.Gone)
    
    0 讨论(0)
  • 2020-12-08 20:28

    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

    0 讨论(0)
  • 2020-12-08 20:42

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

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

    0 讨论(0)
  • 2020-12-08 20:44

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

    Using System.Web 
    

    to the top of the cs file.

    0 讨论(0)
提交回复
热议问题