C
C#15mo ago
palapapa

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

Title
8 Replies
Hazel 🌊💃
Hazel 🌊💃15mo 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
palapapaOP15mo ago
You said [Route("/")] twice?
Hazel 🌊💃
Hazel 🌊💃15mo ago
My bad Fixed it
palapapa
palapapaOP15mo ago
I tested it and [Route("")] doesn't seem to respond to /Sample
Hazel 🌊💃
Hazel 🌊💃15mo 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
palapapaOP15mo 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 🌊💃15mo 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
Accord15mo 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.
Want results from more Discord servers?
Add your server