0

I'm migrating old (bad) code for ASP.NET Web API (.NET Framework 4.5) to ASP.NET Core 6. I must maintain backward compatibility.

There are two methods on controller that are different only by arguments:

public IEnumerable<string> Get([FromUri] string tags, [FromUri] string counter) {...}
public IEnumerable<string> Get([FromUri] string folders, [FromUri] string tags, [FromUri] string counter) {...}

The routing is typical (nothing special):

config.Routes.MapHttpRoute("default",
  "api/{controller}/{id}",
  new { id = RouteParameter.Optional },
  null,
  null });

This works in .NET Framework 4.5, the following requests are routed correctly:

/api/RTData2?tags=0,1,2&counter=762
/api/RTData2?folders=0,1,2&tags=11,12,17&counter=133

How do I make that work with ASP.NET Core? (the client/web side is out of my touch).

Thank you in advance.

The only relevant information I was able to find: Query parameter routing with asp .net core

But that is not really my case.

6
  • And what is the problem? What error do you have? I don't think that there is a big difference between net and net.core routing Commented Feb 21, 2023 at 16:43
  • It throws AmbiguousMatchException. Commented Feb 21, 2023 at 21:13
  • AmbiguousMatchException can be thrown on the server side since you have 2 very similar actions, Why do you need 2 actions with the same name? Do you know that parameters order doesn't matter? Commented Feb 21, 2023 at 21:21
  • That is old legacy code. I cannot change client side. URLs must remain the same. Commented Feb 21, 2023 at 22:45
  • Somehow older ASP.NET (.NET Framework) does the routing based on query arguments only. Commented Feb 21, 2023 at 22:46

1 Answer 1

0

you have to actions with the same name "get", maybe you need to add action name to separate them, something like this

[NonAction]
private IEnumerable<string> Get(string tags, string counter) {...}

[HttpGet("~/api/RTData2")] 
public IEnumerable<string> Get(string tags,  string counter, string folders  ) {

if(string.IsNullOrEmpty(folders)) return Get(tags,counter);
...your usual code

}

routing doesn't check how many query string parameters you url has. It will call the action without any parameters as well.

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

1 Comment

That is what I'm using, combining two (or more) Web API methods into one with combination of nullable arguments. Then invoke appropriate code based on nullables. Thank you all for sharing ideas.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.