Web Api 2 Post - UrlHelper.Link should not return null

I have a basic setup of web API 2 with some basic routing.

Below is the default route and message for inserts. When I call the message, the record is created perfectly in the database, but the call "CreatedAtRoute" returns message 500:

ExceptionMessage: "UrlHelper.Link must not return null." ExceptionType: "System.InvalidOperationException"

Why am I getting this error?

[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); } 
+18
asp.net-web-api2
Apr 14 '15 at 19:45
source share
1 answer

Since you use attribute routing, you must specify your route. [Route ("api / books / {id}", Name = "GetBookById")]

and use the name of the route in the url.link () call

see details here. http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#route-names

+32
Apr 15 '15 at
source share