簡體   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