2

How can I map multiple urls to one action method? For example http://localhost:10000/api/ABC and http://localhost:10000/api/ABCDCD will map to same action name because both starts with ABC. I can't add ABC and ABCDCD as routes because I would not know in advance what user will pass.

Here is my current method

[HttpGet]
[Route("~/api/ABC/")]
public HttpResponseMessage GetData()
{
}

I tried using {ver} but it did not work.

1 Answer 1

3

You can use patterns.

[HttpGet]
[Route("~/api/ABC{data}")]
public IActionResult GetData(string data)
{
    return Ok(data);
}

When you request https://localhost/api/ABCEXAMPLE the value of string data would be EXAMPLE. You can even do not use it if you don't want.


UPD:

The better solution is to use regex:

[HttpGet]
[Route("~/api/{s:regex(^ABC.*)}")]
public IActionResult GetData()
{
    return Ok();
}

enter image description here

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

Comments

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.