No route matches the supplied values for HTTP POST

I am making a POST method. The following code takes in an object, takes the data from the object that it wants, then formats it into a new object and saves that to the database. I use the following code:

        [HttpPost]
        public async Task<IActionResult> Create (Course course) {

            // Generate the object "newCourse" from the data I want

            // Save changes
            _context.Courses.Add(newCourse);
            await _context.SaveChangesAsync();

            // Return 201
            return CreatedAtAction(nameof(newCourse), newCourse);
        }

I get the following error:

System.InvalidOperationException: No route matches the supplied values.
   at Microsoft.AspNetCore.Mvc.CreatedAtActionResult.OnFormatting(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsyncCore(ActionContext context, ObjectResult result, Type objectType, Object value)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor.ExecuteAsync(ActionContext context, ObjectResult result)
   at Microsoft.AspNetCore.Mvc.ObjectResult.ExecuteResultAsync(ActionContext context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultAsync(IActionResult result)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeNextResultFilterAsync[TFilter,TFilterAsync]()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

From I understand from other stack overflow questions, this error was (in 2016) caused by a bug with routing methods that ended in “Async” that was fixed. However, I am on the newest version and my method does not contain the phrase “Async”, so this cannot be the problem I have. I believe it to be an issue with the CreatedAtAction. What might I be doing wrong?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Your issue is because you are providing a concrete class as the object parameter an it needs the object so

return CreatedAtAction(nameof(newCourse), newCourse);

should be

return CreatedAtAction(nameof(newCourse), new { newCourse });

Method 2

I think that we have a problem with the first argument. The second argument is passed correctly.

Method CreatedAtAction(string actionName, object value) accepts two arguments, the first one is the name of an action. Seems like there’s no newCourse method inside of the same controller.

CreatedAtAction accepts action name to create a Location header that is usually returned along with 201 response code following REST conventions. It is an uri which can be used to retrieve newly created resource.

Here is an example if you have two methods, one for Get and another one for Create. Assumed that you have some id argument for Get method.

[HttpGet]
public async Task<IActionResult> Get(string id) {
     ...
}

[HttpPost]
public async Task<IActionResult> Create (Course course) {
    var newCourse = ...
    
    _context.Courses.Add(newCourse);
    await _context.SaveChangesAsync();

    return CreatedAtAction("Get", new {id = course.Id}, newCourse);
}

CreatedAtAction documentation

Method 3

I fixed the issue by changing

return CreatedAtAction(nameof(newCourse), newCourse);

to

return CreatedAtAction("Create", newCourse);

I believe that the nameof(newCourse) was not the correct actionName parameter for the overload I was trying to use. Overload docs page. I changed it to the name of the function.
note- it did not work when i tried the string “Created”, only “Create”.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x