C
C#16mo ago
palapapa

❔ Is there any difference between `[Route("")]` and `[Route("/")]`?

Title
8 Replies
Hazel 🌊💃
Hazel 🌊💃16mo ago
If I remember correctly: - [Route("/")] will only respond to requests with an exact path of /. - [Route("")] will respond to requests made to the root path (e.g. / or /Sample).
palapapa
palapapaOP16mo ago
You said [Route("/")] twice?
Hazel 🌊💃
Hazel 🌊💃16mo ago
My bad Fixed it
palapapa
palapapaOP16mo ago
I tested it and [Route("")] doesn't seem to respond to /Sample
Hazel 🌊💃
Hazel 🌊💃16mo ago
[Route("api/[controller]")]
public class MyController : ControllerBase
{
// This action should respond to requests like /api/My or /api/My/SomeAction
[HttpGet("")]
public IActionResult SomeAction()
{
// ...
}

// This action should only respond to requests with the root path, e.g., /
[HttpGet("/")]
public IActionResult RootAction()
{
// ...
}
}
[Route("api/[controller]")]
public class MyController : ControllerBase
{
// This action should respond to requests like /api/My or /api/My/SomeAction
[HttpGet("")]
public IActionResult SomeAction()
{
// ...
}

// This action should only respond to requests with the root path, e.g., /
[HttpGet("/")]
public IActionResult RootAction()
{
// ...
}
}
palapapa
palapapaOP16mo ago
Just tried it and [HttpGet("")] didn't respond to /api/My/SomeAction. It still acted like [HttpGet("/")] I know that when you use [HttpGet("/")] on an action method then its template string doesn't get combined with the template string on the controller
Hazel 🌊💃
Hazel 🌊💃16mo ago
I guess I don't remember correctly then catshrug sorry I couldn't be of more help Someone else might come along shortly
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?