1
votes

{"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nPostperson on type choreManagerWEBAPIproject.Controllers.peopleController\r\nPostRegister on type choreManagerWEBAPIproject.Controllers.peopleController","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"}

The above error leads me to believe that my RouteConfig is incorrect.

Right now I'm making the following call locally:

$.ajax({
    url: "http://localhost:17438/api/people/PostRegister/",
    type: "POST",
    data: JSON.stringify({ name: 'Jeff', email: '[email protected]', phone: '5551212', carrierName: 'SPRINT'}),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function (result) {
        alert(result);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
        alert("Status: " + textStatus); alert("Error: " + errorThrown);
    }
});

When this gets called the error displays: Internal Server Error

As you can see I'm calling the PostRegister.

    [ResponseType(typeof(string))]
    [HttpPost]
    [HttpOptions]
    public string PostRegister([FromBody] newUserRegistration newReg)
    {
        ...

        var json = JsonConvert.SerializeObject(parent.familyID);
        return json;
    }

My RouteConfig:

        routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
        routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
        routes.MapHttpRoute("DefaultApiWithActionAndId", "Api/{controller}/{action}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
        routes.MapHttpRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint("Get") });
        routes.MapHttpRoute("DefaultApiPost", "Api/{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint("Post") });

I'm thinking that it should be going to this config:

DefaultApiWithAction

When I debug into the WebAPI controller it goes to this line in the people controller:

public class peopleController : ApiController
{
private FamilyChoreManagerEntities db = new FamilyChoreManagerEntities();
...
}

But then I get the error message. And then it stops debugging.

I do have 2 methods for POST:

public void Postperson(person person)
public string PostRegister([FromBody] newUserRegistration newReg)
1

1 Answers

0
votes

I think that your request URL is wrong. Web API 2 will try to match your controller methods using some conventions. By default, it looks for a case-insensitive match with the start of the controller method name. In this case it is identifying the POST methods by using the Post* method names. However, I assume it is then failing to find a matching route so is listing the two availble POST methods that it can find. If your controller method was PostPostRegister it would find it I believe. However this would not make a lot of sense in terms of a naming convention!

You should be asking for Register so Web API can then find a matching route of PostRegister. i.e. your url should be something

url: "http://localhost:17438/api/people/Register/"