I'm trying to build a test web API controller, just for the purpose of understanding web API. I'm wrote the following web API controller:
[Route("api/books")]
public class BookController : Controller
{
public IEnumerable<Book> Get()
{
List<Book> books = new List<Models.Book>();
books.Add(new Book { Title = "Test Title 1", Author = "Test Author 1", ISBN = "Test ISBN 1" });
books.Add(new Book { Title = "Test Title 2", Author = "Test Author 2", ISBN = "Test ISBN 2" });
return books;
}
[HttpGet("{id}")]
public Book Get(int id)
{
return new Book { Title = "Test Title 1", Author= "Test Author 1", ISBN="Test ISBN 1", PublishingDate = DateTime.Today.AddYears(-90).AddDays(-73) };
}
//[HttpPost]
//public IActionResult Post([FromBody]Book book)
//{
// string str = "Post request received";
// return new JsonResult(str);
//}
[HttpPost]
public IActionResult Post([FromBody]Book book)
{
//return CreatedAtRoute("abi/books", new { controller = "Book", id = 1 }, book);
//return CreatedAtRoute("Get", new { controller = "Book", id = 1 }, book);
//return CreatedAtRoute("Get", book);
//return CreatedAtRoute("abi/books", book);
return CreatedAtRoute("abi/books/1", book);
}
}
The Get
and Get(id)
methods work fine. The commented port method also works fine. In the other post method, I'm trying to use the CreatedAtRoute
method. When I call this action using the postman tool, it returns 500 internal server error
. I tried several alternatives for the method parameters (as you can see in the comment return statements) but non of them worked.
This is the detailed error that I get:
System.InvalidOperationException: No route matches the supplied values.
at Microsoft.AspNetCore.Mvc.CreatedAtRouteResult.OnFormatting(ActionContext context)
at Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor.ExecuteAsync(ActionContext context, ObjectResult result)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__32.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__29.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__6.MoveNext()
Any help is highly appreciated
abi/books/1
instead ofapi/books/1
. So the error is accurate as there is no route that starts withabi
– Nkosi