user1477388
user1477388

Reputation: 21430

WebAPI Method Reads Multiple Actions Found

I have two methods, both of which work perfectly one their own (i.e. when one of them is commented out) but when both are in the program, I get this error:

{"Message":"An error has occurred.","ExceptionMessage":"Multiple actions were found that match the request: \r\nAddUserToGroup on type WebAPI_POC1.Controllers.Admin.AccountController\r\nGetRolesForUser on type WebAPI_POC1.Controllers.Admin.AccountController","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.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()"}

Here are the two methods in my AccountController:

public bool AddUserToGroup(AddUserToGroupBindingModel model)
{
    bool result = false;
    if (!ModelState.IsValid)
    {
        return result;
    }

    var user = UserManager.FindByName(model.UserEmail);

    if (user != null)
    {
        var tmp = UserManager.AddToRole(user.Id, model.GroupName);
        result = ((tmp.Succeeded) ? true : false);
    }

    return result;
}

[HttpPost]
public IEnumerable<string> GetRolesForUser(GetUserGroupsBindingModel model)
{
    var user = UserManager.FindByName(model.UserEmail);
    List<string> roles = new List<string>();

    if (user != null)
    {
        roles = new List<string>(UserManager.GetRoles(user.Id));
    }

    return roles;
}

Important Detail: I am using a different Route config like so:

config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{namespace}/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));

The code is from http://blogs.msdn.com/b/webdev/archive/2013/03/08/using-namespaces-to-version-web-apis.aspx which I implemented so that I could use namespaces to separate my different controllers.

Why can't WebAPI find these two different controller actions which have different names, different signatures and different return types?

Upvotes: 0

Views: 2185

Answers (3)

Vivek
Vivek

Reputation: 1

[Route("api/Controller-Name/Method-Name")] [HttpPost]

Upvotes: 0

user1477388
user1477388

Reputation: 21430

I fixed this by changing my route like so. Thanks to everyone for their help.

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{namespace}/{controller}/{action}/{id}",
            defaults: new { action = "Index", id = RouteParameter.Optional }
        );

config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));

Upvotes: 1

Gerben Rampaart
Gerben Rampaart

Reputation: 9945

Check your WebApiConfig, it probably has something like this?

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{Id}",
            defaults: new { id = RouteParameter.Optional }
        );

Have you tried your get operation like this? "/api/admin/account/GetRolesForUser/[email protected]"

If that works maybe adding a [FromUrl] to your operation like so:

public bool GetRolesForUser([FromUrl] string userEmail)

Upvotes: 1

Related Questions