我的控制器:
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 資源
我在這里做錯了什么?
uj5u.com熱心網友回復:
你必須修復一個路由模板,并修復一個動作路由
[Route("{id}")]
public IHttpActionResult GetComments(int Id)
但您也可能必須修復控制器路由,因為它源自 ApiController,而不是控制器
[Route("~/api/[controller]/[action]")]
public class CommentController : ApiController
uj5u.com熱心網友回復:
選項 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);
}
在這種情況下,對“”的 GET 請求api/Comment/GetComments/1將映射到 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”。此約定僅適用于GET、POST、PUT、DELETE、 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...”方法。
uj5u.com熱心網友回復:
您需要在 url 中發送 id。例如:https://localhost:44361/api/comment/getcomments?id=1234
uj5u.com熱心網友回復:
[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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/493137.html
標籤:C# 网 asp.net-mvc asp.net-web-api asp.net-web-api2
