0
votes

My simple web api application keeps returning "500 Internal Server Error" with the message "Multiple actions were found that match the request" and I thought this must be a routing problem.

Here is a gist with the UserController and the WebApiConfig files that can have an impact on this issue.

I went with the web api convention when I created my methods and I don't want to use attributes on the methods to specify type and route.

If I use a Route attribute on the POST method, it will work, but it doesn't make sense to me why it doesn't work w/o that attribute since I use the naming convention for a POST method.

ex that works:

    [Route("api/user")]
    public IHttpActionResult Post([FromBody]User user)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest("Model state not valid!");
        }

        if (db.Users.ToList().Any(u => u.Username.ToLowerInvariant().Equals(user.Username.ToLowerInvariant())))
        {
            return BadRequest("Username already exists in the database!");
        }

        db.Users.Add(user);
        db.SaveChanges();

        return Ok($"Added user {user.Username}");
    }

Any thought is welcomed.

1
Why are you using HTTP request types as your method name?Atanu Roy
What is the name of your controller, what are the names of the other actions and what does your request URI look like?Domysee
The name of the controller is "UserController", the other actions are included in the Gist attached here gist.github.com/anonymous/245da58b4bc93cd462bf and the request URI is: localhost:2382/api/userAlin Argintaru

1 Answers

1
votes

Clearly the problem is with your Dispose() method. This method is conflicting with your Post() action and WebAPI action selector can't decide which action to choose.

Just make Dispose() public to protected.