3

Why this doesn't work? I get error: System.InvalidOperationException: Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

public class ConfigUpdateController: ApiController
{
    [HttpPut]
    [Route("api/device/{serial}/config")]
    public IHttpActionResult Update(
        [FromUri] string serial,
        [FromBody] Configuration configuration)
    {

    }
}

public class ConfigQueryController: ApiController
{
    [HttpGet]
    [Route("api/device/{serial}/config")]
    public IHttpActionResult Get(
        [FromUri] string serial)
    {

    }
}

The reason why i want to have methods for same resource in separate controllers is decoupling queries from commands.

EDIT

To be honest, it's ample code to illustrate my problem, so please don't bother commenting controllers naming ect. It's not important in context of my question.

EDIT 2

I've found here web-api overview thet routing has 3 phases:

Routing has three main phases: Matching the URI to a route template. Selecting a controller. Selecting an action.

So it seems this does not work because controller can't be resolved and method verb (PUT, GET) are not even checked? O_o

Pujubuju
  • 119
  • 1
  • 7
  • 1
    Methods have the same signature and same route which is causing the issue. Either change the signature of your method or the route. It also wouldn't do you harm to use proper naming of your controllers. – hbulens Jul 05 '16 at 05:54
  • your route is not based on controller – Sandip Bantawa Jul 05 '16 at 05:54
  • ASP.NET MVC will allow you to have two actions with the same name, but not with the same signature,have a look at this answer http://stackoverflow.com/questions/9552761/get-and-post-methods-with-the-same-action-name-in-the-same-controller – Andre Lombaard Jul 05 '16 at 05:59
  • Two methods with same action name is not my target. And it's not mvc, it's web api. – Pujubuju Jul 05 '16 at 06:02
  • Hi @hbulens simply stated that your controller can be given a better name as a good gesture, there is no need to be rude, remember that you asked us for help on an issue that in my view you can very quickly get an answer to with a simple google search. – Andre Lombaard Jul 05 '16 at 06:03
  • @user65439 What are you talking about? Comment wasn't meant to be rude and I don't think it is. – hbulens Jul 05 '16 at 06:06
  • Let's stay on track here guys... @Pujubuju do you have any routing configured in your `WebApiConfig` file? – timothyclifford Jul 05 '16 at 06:09
  • Yep, it's only configuration.MapHttpAttributeRoutes(). No default routes or anything else. – Pujubuju Jul 05 '16 at 06:12
  • And no other routes configured anywhere else in your code? – timothyclifford Jul 05 '16 at 06:13
  • Ofc there are other controllers with attribute routing, but non of them match route "api/device/...". – Pujubuju Jul 05 '16 at 06:16
  • Can you confirm `configuration.MapHttpAttributeRoutes()` is being executed when your app loads? – timothyclifford Jul 05 '16 at 06:17
  • Yes. If i modify one of the routes to be slightly different from another it works fine, but it makes my api look ugly. – Pujubuju Jul 05 '16 at 06:20

2 Answers2

0

Read the error carefully, then look at your attribute routing. You have identical URLs for two different actions. Program would have no idea which action to execute.

dvsoukup
  • 1,586
  • 15
  • 31
  • 2
    They are not. One is GET and second PUT. Router should distinguish such obvious difference, don't You think? – Pujubuju Jul 05 '16 at 05:55
0

use [FromRoute] instead of [FromUri] annotations

ravindra
  • 322
  • 3
  • 14