0

I have two different webapi controllers with actions as shown below. For some reason i keep getting "Multiple controller types were found that match the URL...." exception. Can anyone help me to figure out what i am doing wrong here?

ControllerA

[HttpGet]
[Route("clips/{Id}", Name = "GetById")]
public async Task<HttpResponseMessage> Get(string Id)
{

}

*Id is alphanumeric and it always starts with a number

ControllerB

[HttpGet]
[Route("clips/product", Name="GetXProducts")]
public async Task<HttpResponseMessage> GetXProducts([FromUri]SearchCriteria searchCriteria)
{

}

Thanks

2 Answers 2

2

You can apply a regular expression to both routes to ensure that it is chosen when the appropriate parameter is passed:

[Route(@"clips/{Id:regex([0-9]*[A-Z0-9]*)}", Name = "GetById")]

You will need to apply a similar expression to the other route to exclude these hits.

But I agree with @ryancdotnet that you should probably reevaluate your routes.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But the requirement is what i have mentioned in the question. and there are couple of other routes which are like i) clips/AAA, ii)clips/BBB etc., is there any other way to achieve these kind of requirement please?
Im not sure I understand the full scope of the required URL scheme, but if you can't handle the different routes with regular expressions, you can always write a custom route restraint. See: asp.net/web-api/overview/web-api-routing-and-actions/…. Note that this example is for Web API, but similar classes exist in System.Web.Routing (IRouteConstraint).
0

It looks like the issue is because your {id} on Controller A is of type string, and the Route for Controller B is essentially the same thing, as product is also a string, therefore Routing does not know how to differentiate between the two operations.

1 Comment

I would try reconsidering your URI path to see if there is a better resource representation of the objects you are wanting to return. Coming from a REST perspective, I'm not sure if /clips/product is a good resource representation. Perhaps something like /clipproducts/ instead?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.