Web Api Request.CreateResponse HttpResponseMessage

2019-01-17 10:36发布

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?

6条回答
Root(大扎)
2楼-- · 2019-01-17 11:13

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

查看更多
三岁会撩人
3楼-- · 2019-01-17 11:14

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

Using System.Web 

to the top of the cs file.

查看更多
来,给爷笑一个
4楼-- · 2019-01-17 11:17

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

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

查看更多
欢心
5楼-- · 2019-01-17 11:21

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

查看更多
Juvenile、少年°
6楼-- · 2019-01-17 11:27

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

using System.Net.Http;
查看更多
仙女界的扛把子
7楼-- · 2019-01-17 11:32

You can use

 HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.Gone)

instead of

 Request.CreateResponse(HttpStatusCode.Gone)
查看更多
登录 后发表回答