C
C#14mo ago
Ikinidae

❔ Controller routing with dynamic parameter

Hi all guys! That's my first post so I don't know if I'm doing anything wrong, in case let me know! I have an api project with a lot of api that are gonna be used for both a website (control panel) and a mobile app (for customers). My controllers are all like this:
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("1.0")]
[ApiController]
public class AgentController : ControllerBase
{
// my api
}
[Route("api/v{version:apiVersion}/[controller]")]
[ApiVersion("1.0")]
[ApiController]
public class AgentController : ControllerBase
{
// my api
}
that means that my url looks like this: https://localhost:5000/api/v1/Agent What I need is to specify in the url if that api will be used in app or web, so that my url will look like this: https://localhost:5000/api/web/v1/Agent That parameter has to be specified for every api just like [HttpGet("GetAgentById")]. I obviously searched on web but I haven't found anything 😦 Thanks a lot ❤️
5 Replies
friedice
friedice14mo ago
Why not create 2 controllers, one for the web and one for mobile? Is there a reason you want to put everything in one generic controller?
Ikinidae
IkinidaeOP14mo ago
I already have all the api there, that's a solution, but I was just curious about the existence of that option!
BananaPie
BananaPie14mo ago
Can't you just look at the HttpContext.Request.Headers['User-Agent'] to see if the request is from mobile or pc? You can do a custom attribute to check that and redirect.
Ikinidae
IkinidaeOP14mo ago
That's not the point, my boss just wants to know if an api is for web or app reading its url 🤷‍♀️
Accord
Accord14mo 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