Web API需要显式设置可选值,即使对于可空类型和类也是如此.
使用默认值为可选参数:
[Route("Rest/GetName/{name?}")] public IHttpActionResult GetName(string name = null) { // cut - code here is trivial but long, I just fill in an object to return as Json code return Json(myObject); }
不要忘记路由注册:
httpConfig.MapHttpAttributeRoutes()