0

I'm having a problem with the following methods:

[Authorize]
[HttpGet]
[ResponseType(typeof(IEnumerable<Index.Model>))]
public async Task<IHttpActionResult> GetArchives()


[Authorize]
[HttpGet]
[ResponseType(typeof(Get.Query))]
public async Task<IHttpActionResult> GetArchive(Get.Query query)

It is giving me the following error:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>
        Multiple actions were found that match the request: GetArchives on type IAP.api.Controllers.ArchivesController GetArchive on type IAP.api.Controllers.ArchivesController
    </ExceptionMessage>
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <StackTrace>
         at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext) at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
    </StackTrace>
</Error>

I've read through the other solutions for people asking the same question, but haven't worked for me (more specifically this one). I'm looking for a solution where I don't need to change my Web Api default REST routing as I will have to change it in the client also.

Thanks,

Community
  • 1
  • 1
Thomas Monte
  • 137
  • 1
  • 11

2 Answers2

1

You can use [ActionName] attribute on top of action methods to specify some different name

[ActionName("archives")]

[ActionName("archive")]

Now from the client side you can call by its action name. Read Here for more: http://www.codeguru.com/csharp/.net/using-custom-action-names-in-asp.net-web-api.htm

Hasta Tamang
  • 2,205
  • 1
  • 18
  • 17
1

It's actually problem with same routings. You can try to use Route attribute to change the route for one of these methods but not the whole routing scheme. Also with these attribute you can try to not change route name (GetArchives) but use additional route constraint for your second method. And then it will be something like this:

/archives/GetArchives
/archives/GetArchives/GetQueryConstraint

To achieve this place [Route("{query}")] on the second method. If this is your custom object then you will need to create your custom constraint and specify it

[Route("{query:constraintType}")]

Andrew
  • 1,474
  • 4
  • 19
  • 27
  • Thanks for the info! I ended up having to change the method signature a bit and add this to the top of the GetArchive method: `[Route("api/archives/{id:int}")] public async Task GetArchive(int id) { var query = new Get.Query() { Id = id }; ... }` – Thomas Monte Jan 06 '16 at 18:05
  • @user1617845, glad to help you – Andrew Jan 06 '16 at 19:29