I have this function in my Web Service that add a 'addlocalidade' in my table TBLocalidade in SQL Server.
[HttpPost]
[Route("api/insertlocalidade")]
[ResponseType(typeof(TBLocalidade))]
public async Task<IHttpActionResult> insertlocalidade([FromBody] TBLocalidade addlocalidade)
{
objapi.Configuration.LazyLoadingEnabled = false;
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
objapi.TBLocalidade.Add(addlocalidade);
await objapi.SaveChangesAsync();
return CreatedAtRoute("DefaultApi", new { id = addlocalidade.idLocalidade }, addlocalidade);
}
If I remove the route, it works perfectly, but when I add this route: [Route("api/insertlocalidade")], it doesn't work good, I tested it with httpRequester, it was possible to add the value in database, but it return this error message:
{"Message":"An error has occurred.","ExceptionMessage":"UrlHelper.Link must not return null.","ExceptionType":"System.InvalidOperationException","StackTrace":" em System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult
1.Execute()\r\n em System.Web.Http.Results.CreatedAtRouteNegotiatedContentResult1.ExecuteAsync(CancellationToken cancellationToken)\r\n em System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada ---\r\n em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n em System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada ---\r\n em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n em System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext()\r\n--- Fim do rastreamento de pilha do local anterior onde a exceção foi gerada ---\r\n em System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n em System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n em System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}
Anyone can please help me to solve this error?