繁体   English   中英

无法从 ASP.NET Web API 获得任何响应

[英]Can't get any response from ASP.NET Web API

我的控制器:

public class CommentController : ApiController
{
    private readonly ICommentRepository _commentRepository;

    public CommentController(ICommentRepository commentRepository)
    {
        _commentRepository = commentRepository;
    }

    public IHttpActionResult GetComments(int Id)
    {
        var comments = _commentRepository.GetComments(Id);
        return Ok(comments);
    }
}

WebApiConfig 文件:

public static void Register(HttpConfiguration config)
{
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
}

Web API 在一个单独的项目中:

在此处输入图像描述

当我输入https://localhost:44361/api/comment/getcomments

我收到此错误:

未找到与请求 URI 匹配的 HTTP 资源

我在这里做错了什么?

你必须修复一个路由模板,并修复一个动作路由

[Route("{id}")]
public IHttpActionResult GetComments(int Id)

但您也可能必须修复控制器路由,因为它源自 ApiController,而不是控制器

[Route("~/api/[controller]/[action]")]
public class CommentController : ApiController

选项 1:您可以尝试使用按操作名称路由(链接中的文档并在此处解释)

使用默认路由模板,Web API 使用 HTTP 动词来选择操作。 但是,您也可以创建一个路由,其中​​操作名称包含在 URI 中:

routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

在此路由模板中,{action} 参数命名控制器上的操作方法。 使用这种路由风格,使用属性来指定允许的 HTTP 动词 例如,假设您的控制器具有以下方法:

public class CommentController : ApiController
{
    [HttpGet] // Attribute to specify the allowed HTTP verbs
    public string GetComments(int id);
}

在这种情况下,“ api/Comment/GetComments/1 ”的 GET 请求将映射到 GetComments 方法。

选项 2:您还可以使用路由表来确定调用哪个操作,框架使用路由表。

routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);

一旦找到匹配的路由,Web API 就会选择控制器和操作:

  • 为了找到控制器,Web API 将“Controller”添加到 {controller} 变量的值中。
  • 为了找到动作,Web API 会查看HTTP 谓词,然后查找名称以该 HTTP 谓词名称开头的动作 例如,对于 GET 请求, Web API 会查找以Get ”为前缀的操作,例如“ Get Comment”或“GetAllComments”。 此约定仅适用于GETPOSTPUTDELETE 、 HEAD 、 OPTIONS 和 PATCH 动词。

样本:

public class CommentController : ApiController
{
    public IEnumerable<Comment> GetAllComments() { }
    public Comment GetCommentById(int id) { }
    public HttpResponseMessage DeleteComment(int id){ }
}

以下是一些可能的 HTTP 请求,以及为每个请求调用的操作:

HTTP 动词 | URI 路径 | 行动 | 范围

  • 获取 | api/评论| 获取所有评论 | (没有任何)
  • 获取 | api/评论/4 | GetCommentById | 1
  • 删除 | api/评论/4 | 删除评论 | 1
  • 发布 | api/评论| (不匹配)

请注意,URI 的{id}段(如果存在)映射到操作的id参数。 在这个例子中,控制器定义了两种 GET 方法,一种带有 id 参数,一种没有参数。

另外,请注意 POST 请求将失败,因为控制器没有定义“Post...”方法。

您需要在 url 中发送 id。 例如:https://localhost:44361/api/comment/getcomments?id=1234

[RoutePrefix("api/Comment")] //routeprefix bind before url every action//you can use Route also 
public class CommentController : ApiController
{
    private readonly ICommentRepository _commentRepository;

    public CommentController(ICommentRepository commentRepository)
    {
        _commentRepository = commentRepository;
    }

    [Route("{id:int}")] //or//[Route("~/api/**meaningfulRelatedname**/{id:int})]
    [HttpGet]
    public IHttpActionResult GetComments(int Id)
    {
        var comments = _commentRepository.GetComments(Id);
        return Ok(comments);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM