0

I have a problem of url routing in ASP.NET Core web api. When the URL contins dot (.) it returns 404 not found. For example,

http://localhost:9030/api/test/109/[email protected]

The code that doesn't work with email but work with no dot

public class TestController : Controller{
{
    [HttpGet]
    [Route("{id:int}")]
    public IActionResult Get(int id)
    {
        //whatever
    }

    [HttpGet]
    [Route("{id:int}/{name:alpha}")]
    public IActionResult Get(int id, string name)
    {
        //whatever
    }
}

However, this works

http://localhost:9030/api/test/getbyname/109/[email protected]

public class TestController : Controller{
{
    [HttpGet("GetById/{id}")]
    public IActionResult Get(int id)
    {
        //whatever
    }

    [HttpGet("GetByName/{id}/{name}")]
    public IActionResult Get(int id, string name)
    {
        //whatever
    }
}

How to solve that problem?

3
  • 2
    [email protected] would not be valid regardless. Did you forget to url encode the @? urlencoder.org Commented Jan 3, 2019 at 12:05
  • No problem with @. The problem is dot (.). I'm using IIS 8.5 Commented Jan 3, 2019 at 12:13
  • It may work, but you shouldn't be passing @ like that. You should url encode it. Commented Jan 3, 2019 at 12:21

1 Answer 1

3

Your problem is the

[Route("{id:int}/{name:alpha}")]

You are saying only accept alphabet characters which means a-z and A-Z not including any special characters. you are best using a regex to validate if it is always an email address.

try this

[Route("{id:int}/{name}")]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. It works now. This is the complete explanation learn.microsoft.com/en-us/aspnet/core/fundamentals/… and this is for custom constraints sample c-sharpcorner.com/article/creating-custom-routing-constraint

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.