Подтвердить что ты не робот

Web Api Request.CreateResponse HttpResponseMessage no intellisense VS2012

По какой-то причине Request.CreateResponse теперь "красный" в VS2012, и когда я наводил курсор на использование, IDE сообщает

Не удается разрешить символ "CreateResponse"

Вот класс ApiController:

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);
        }
    }
}

Конечно, код компилируется и работает, он просто очень раздражает, видя все "красные" на моем экране. Кроме того, intellisense не работает сейчас, когда я набираю Request.CreateResponse. Это также сработало, но я начал разрабатывать другие части для своего API и просто вернулся к контроллерам зданий, поэтому не знаю, что произошло.

Любые мысли?

4b9b3361

Ответ 1

Поскольку этот метод расширений находится в System.Net.Http, вам просто нужно включить его в свои заявления.

using System.Net.Http;

Ответ 2

Вам нужно добавить ссылку на System.Net.Http.Formatting.dll. Там определен метод расширения CreateResponse.

Ответ 3

Это потому, что вам нужно сделать:

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);
        }
    }
}

Обратите внимание на this.
В моем случае компилятор теперь получает его. Пила пример здесь

Ответ 4

Вы можете использовать    Результат HttpResponseMessage = новый HttpResponseMessage (HttpStatusCode.Gone)

вместо

 Request.CreateResponse(HttpStatusCode.Gone)

Ответ 5

Добавить ссылку на System.Web.Http в проект. Затем добавьте

Using System.Web 

в начало файла cs.