0

I have controller with one method. Routing covers next scenarious:

1. userId="1" ,userEmail=null https://localhost:44371/api/customers/1
1. userId="1" ,userEmail="1" https://localhost:44371/api/customers/1/1

The main question how to send request when userId=null, userEmail="1". Use %20 and request something like https://localhost:44371/api/customers/%20/1? What is right way?

[HttpGet("{userId}")]
[HttpGet("{userId}/{userEmail}")]
[ApiController]
public class CustomersController : ControllerBase
{
    public JsonResult GetCustomer(string userId, string userEmail)
    {
        return new JsonResult(string.Format("userId: {0}, email: {1} ", userId, userEmail));
    }
}

1 Answer 1

1

You are routing in wrong way so change your code to this because you will need to routing in the action method

[ApiController]
[Route("api/[controller]")]
public class CustomersController : ControllerBase
{
    [HttpGet("{userId}/{userEmail}")]
    public JsonResult GetCustomer(string userId, string userEmail)
    {
        return new JsonResult(string.Format("userId: {0}, email: {1} ", userId, userEmail));
    }
}

So you can request api like this

https://localhost:44371/api/customers/1

Another example

[Route("[controller]/[action]")]
public class ProductsController : Controller
{
    [HttpGet] // Matches '/Products/List'
    public IActionResult List() {
        // ...
    }

    [HttpGet("{id}")] // Matches '/Products/Edit/{id}'
    public IActionResult Edit(int id) {
        // ...
    }
}
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.