✅ Is it possible to access a Controller's URI at runtime?

I'm not well versed in MVC structures, so this very well could be a XY problem. Let me know if it is a bad idea to begin with. I'm trying to create a GET endpoint for /collections which returns Collections. One Collection always contains a nested Model User. On its own, this is not a problem. However, I am creating my API based around application/hal+json, which requires Resources to have a link to point to their exact location. So when accessing a returned Collection, I could use its related link if I wish to access it directly instead. It looks sort of like this: Endpoints: /collections /collections/{uuid}
{
"_links": {
"self": {
"href": "http://foo-api/collections"
}
},
"_embedded": {
"collection": [

{
"fooProp": "I'm a specific collection",
"_links": {
"self": {
"href": "http://foo-api/collections/62fcac86a39a11ef"
}
}
}

]
}
}
{
"_links": {
"self": {
"href": "http://foo-api/collections"
}
},
"_embedded": {
"collection": [

{
"fooProp": "I'm a specific collection",
"_links": {
"self": {
"href": "http://foo-api/collections/62fcac86a39a11ef"
}
}
}

]
}
}
This is all well and good, but I have some concerns with generating the embedded collection's link. If I hardcode it as $"http://foo-api/collections/{uuid}", then I would have to change the domain in every location where I did it. Is it possible to access the CollectionController#GetById(string uuid) somehow and extract its URI at runtime?
9 Replies
dreadfullydistinct
You can use reflection to get at the routing attribute, but that could be quite nasty Or are you asking about how to get the domain?
Luca | LeCarbonator
Either one is of help, since the href should already be a filled out URL At least I don't expect to change my endpoints constantly, so that is okay to be hardcoded for this size of a project
dreadfullydistinct
Ah ok, when I’ve done this before I just grabbed the Host header from the request (via HttpContext.Request.Host) This is required to be set for all HTTP requests and iirc shouldnt be messed up by a reverse proxy But I don’t know if that’s the ideal way to do it, I’ve not seen it being done elsewhere 😅
Luca | LeCarbonator
hmm, I see. I'll read up on the HttpContext then thanks for the advice
Pobiega
Pobiega2mo ago
As dreadfully said, the recommended approach is to extract it from the request. and it should have the correct info even when using reverse proxy, assuming header forwarding is turned on alternatively you create an IOptions<T> object where you store your root-domain
dreadfullydistinct
^ that too, when I’ve deployed stuff like Grafana I’ve had to specify a domain in the config, so I assume they might be doing something like this for their redirects
Luca | LeCarbonator
:HmmNotes: sounds like a plan. These keywords should be enough to solve the post. Do I just mark it with :greencheck: ?
dreadfullydistinct
$solve Idk There’s a slash command Close $close
MODiX
MODiX2mo ago
If you have no further questions, please use /close to mark the forum thread as answered
Want results from more Discord servers?
Add your server