我有一个基本的Web API 2设置与一些基本的路由.
以下是插入的默认路线和帖子.当我调用帖子时,记录在数据库中完美创建,但是“CreatedAtRoute”调用返回500错误,说明:
ExceptionMessage: “UrlHelper.Link must not return null.”
ExceptionType: “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”)]
[Route(“api / books / {id}”,Name =“GetBookById”)]