当前位置: 首页 > 知识库问答 >
问题:

Web Api请求。CreateResponse HttpResponseMessage无intellisense VS2012

濮阳唯
2023-03-14

由于某种原因, 现在在VS2012中是“红色”的,当我将鼠标悬停在IDE的用法上时,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现在无法工作。这也是过去的工作,但我开始开发我的API的其他部分,只是回来构建控制器,所以我不知道发生了什么。

有什么想法吗?

共有3个答案

令狐宜民
2023-03-14

这是因为你必须做到:

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

请注意 ,在我的示例中,编译器现在获取它。我看到这里的例子

廖令
2023-03-14

因为这个扩展方法存在于 中,所以您只需要将它包含在您的usings语句中。

using System.Net.Http;
华泳
2023-03-14

null

 类似资料:
  • 我是ASP的新手。网我对POST请求有问题。也许我的代码错了,我找不到在哪里! 当我尝试post请求时,Postman会向我发送错误消息: "ExceptionType":"System.网。http://http.不支持的媒体类型异常","StackTrace":"àSystem.网。http://http.HttpContent扩展。ReadAsAsync[T](HttpContent内容,类

  • 我是WebApi的新手,我遵循了以下教程https://www.asp.net/web-api/overview/getting-start-with-aspnet-web-api/tutorial-your-first-web-api 一切都按预期工作--我有两个endpoint 我试图理解的是它们如何与控制器中定义的方法相关联。 控制器:

  • 我正在读一本书中关于C#中的响应-消息模式的章节,当我最近遇到一个使用web API的项目时,我注意到了一些相似之处,并想要一些明确的东西。 在书中,作者的代码将请求(在本例中是CustomerRequestService)包装在一个名为CustomerRequestService的类中,该类的工作是处理这些请求(请求只不过是具有与查找客户的服务相关的搜索词或属性的类,而不执行其他非常稀薄/贫乏的

  • 有人能帮我吗? 谢谢

  • 本文向大家介绍jQuery.ajax 跨域请求webapi设置headers的解决方案,包括了jQuery.ajax 跨域请求webapi设置headers的解决方案的使用技巧和注意事项,需要的朋友参考一下 解决跨域调用服务并设置headers 主要的解决方法需要通过服务器端设置响应头、正确响应options请求,正确设置 JavaScript端需要设置的headers信息 方能实现。 1.第一步

  • 我刚刚开始使用Postman测试我正在集成到的API。 我有以下错误不断出现 无效的CORS请求 注意以下几点: API使用承载令牌身份验证(OAuth2)。我有这个工作没有问题 到目前为止,我发现: 使用邮递员与Http POST请求-我没有得到粗体的部分 以防其他人遇到同样的问题,以下是解决方法。在chrome浏览器中https://www.getpostman.com/docs/captur

  • 问题内容: 我正在使用WebAPI v2.2,并且正在使WebAPI使用[FromBody]属性将JSON反序列化到对象上。反序列化的目标类在内部方法上具有[OnDeserialized]属性,如下所示: 我知道事实是此方法中的代码有问题,我已逐步解决并找到了它。对我来说,问题是我一点也不例外。发生的情况是此方法被跳出,异常似乎被忽略了。因为未正确执行此序列化方法,所以调用了我的控制器操作并且未正