Asp.net-Web-Api2
Web Api 2 文章 - UrlHelper.Link 不得返回 null
我有一個帶有一些基本路由的基本 Web API 2 設置。
下面是插入的預設路由和文章。當我呼叫文章時,記錄在數據庫中完美創建,但“CreatedAtRoute”呼叫返回 500 錯誤,說明:
ExceptionMessage:“UrlHelper.Link 不得返回 null。” 異常類型:“System.InvalidOperationException”
為什麼我會收到此錯誤?
[RoutePrefix("api/casenotes")] public class CasenoteController : ApiController... // POST api/Casenote [Route("")] [ResponseType(typeof(client_admission_casenote))] public async Task<IHttpActionResult> Postclient_admission_casenote (client_admission_casenote client_admission_casenote) { Request.GetRequestContext().IncludeErrorDetail = true; if (!ModelState.IsValid) { return BadRequest(ModelState); } db.client_admission_casenote.Add(client_admission_casenote); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = client_admission_casenote.casenote_id }, client_admission_casenote); }
由於您使用的是屬性路由..您必須命名您的路線..即 [Route(“api/books/{id}”, Name=“GetBookById”)]
並在您的 url.link() 呼叫中使用路由名稱
在此處查看詳細資訊.. <http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-names>