本文介绍了Web Api Request.CreateResponse HttpResponseMessage 没有智能感知 VS2012的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,Request.CreateResponse 在 VS2012 中现在是红色",当我将鼠标悬停在使用上时,IDE 说

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

无法解析符号CreateResponse"

这是 ApiController 类:

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

当然,代码确实可以编译并运行,看到屏幕上所有的红色"真的很烦人.此外,当我输入 Request.CreateResponse 时,智能感知现在不起作用.这也曾经有效,但我开始为我的 API 开发其他部分,然后又回到构建控制器,所以我不知道发生了什么.

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.

有什么想法吗?

推荐答案

您需要添加对 System.Net.Http.Formatting.dll 的引用.CreateResponse 扩展方法在那里定义.

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

这篇关于Web Api Request.CreateResponse HttpResponseMessage 没有智能感知 VS2012的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:21