我是 c# 和 web api 的新手。我有一個帶有 Get 方法的 WebAPI 控制器,如下所示: public class peopleController : ApiController { [HttpGet] public IHttpActionResult getAllPeople(string Name, string Age) { //Return something } }我的 WebApiConfig 是這樣的:config.Routes.MapHttpRoute( name: "getAllPeopleApi", routeTemplate: "people", defaults: new { controller = "people", action = "getAllPeople" } );如果我這樣調用我的網址:http://localhost:xxx/people?Name=&Age=。它工作正常。但是當我像所有這些一樣調用時 :http://localhost:xxx/people,,,http://localhost:xxx/people?Name=http://localhost:xxx/people?Age=我收到此錯誤消息:{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:xxxx/......'.","MessageDetail":"No action was found on the controller 'people' that matches the request."}我嘗試設置我的routeTemplate: "people/{Name}/{Age}". 現在當我運行這個 web apiError 404.0 Not Found
2 回答

狐的傳說
TA貢獻1804條經驗 獲得超3個贊
routeTemplate: "people/{Name}/{Age}"
這部分不是 QueryString,這是一個動態路徑。這意味著你的路徑變成Domain/people/SomeName/SomeAge?QueryString=Whatever

動漫人物
TA貢獻1815條經驗 獲得超10個贊
設法通過顯式設置參數Name和Ageto來解決這個問題Null。
public class peopleController : ApiController
{
[HttpGet]
public IHttpActionResult getAllPeople(string Name = null, string Age=null)
{
//Return something
}
}
這將使參數成為可選參數。現在您可以使用或不使用查詢字符串參數來調用控制器。
- 2 回答
- 0 關注
- 201 瀏覽
添加回答
舉報
0/150
提交
取消