[英]Unable to call web api via httpclient
我有一個 controller 具有以下路線:
[HttpGet("/{id}/studentrank")]
public async Task<ActionResult> GetStudentRank(Guid id){
...
}
注意,class 上的裝飾器是:[Route("api/[controller]")]
所以它應該被調用:api/student/{guid here}/studentrank
這在 swagger 中運行良好。 但是,當我按如下方式調用它時,我得到一個內部服務器錯誤,甚至沒有在 controller 中中斷:
var response = await HttpClient.GetAsync($"/api/student/{id}/studentrank");
知道可能缺少什么嗎?
HttpGet 不包含路由參數。 假設該方法在 StudentController class 中,您應該這樣做:
[HttpGet]
[Route("GetStudentRank/{id}")]
public async Task<ActionResult> GetStudentRank(Guid id){
...
}
var response = await HttpClient.GetAsync($"{BaseUrl}/Student/GetStudentRank/{id}");
我認為,因為它是Guid
而不是string
,所以必須在路由中聲明類型,或者如果路由應該與 class 上的路由組合,則路由不能以斜杠開頭:
[HttpGet("{id:guid}/studentrank")]
public async Task<ActionResult> GetStudentRank(Guid id){
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.