Get route information on controller

In my response model, I have stuff like method and path of the route. Is there any classes or methods I can call to inject that into the function? Example
class FooController : ControllerBase {
[HttpGet] // I know it's a get here, but not during runtime
async Task<ActionResult<CoolioResponse>> Bar(
// What to inject here? HttpContext?
) {

}
}
class FooController : ControllerBase {
[HttpGet] // I know it's a get here, but not during runtime
async Task<ActionResult<CoolioResponse>> Bar(
// What to inject here? HttpContext?
) {

}
}
30 Replies
Angius
Angius3mo ago
Not sure what you mean The method will be GET, as the attribute says The path will be what you define there
Sun「無用」
Sun「無用」OP3mo ago
yes, but it's not in the code, I can't use it as a variable or return it to the user
Pobiega
Pobiega3mo ago
yeah just look at HttpContext you dont need to inject it, its a property on the controllerbase
Sun「無用」
Sun「無用」OP3mo ago
async Task<ActionResult<CoolioResponse>> Bar(
// What to inject here? HttpContext?
) {
return new CoolioResponse
{
Path = ??,
Method = ??,
};
}
async Task<ActionResult<CoolioResponse>> Bar(
// What to inject here? HttpContext?
) {
return new CoolioResponse
{
Path = ??,
Method = ??,
};
}
Like that
Batuhan
Batuhan3mo ago
U can use nameof()
Angius
Angius3mo ago
If that's the data you need to return nameof gets you the name of... the thing Does not give you the path So, no, you can't use nameof There's the UrlHelper class that has an .Action method, IIRC You give it the controller, the action method name, and it produces the path
Sun「無用」
Sun「無用」OP3mo ago
I'm lost here, lol
Angius
Angius3mo ago
I'm as lost as you, trying to figure out why you'd want to return "yes, this is a GET endpoint with a path of /foo/bar" from an endpoint handling a GET method at the path /foo/bar It's kinda obvious that calling GET /foo/bar will be... the GET method and the /foo/bar path
Sun「無用」
Sun「無用」OP3mo ago
bc I just want the info, most users are kinda dumb :d
Angius
Angius3mo ago
I've no clue why you'd want it
Pobiega
Pobiega3mo ago
its all in HttpContext
Angius
Angius3mo ago
It's like sending an email to [email protected] and getting a reply "you sent an email to [email protected]"
Sun「無用」
Sun「無用」OP3mo ago
also I'm gonna do stuff like
[HttpPut]
[HttpPatch]
async Task meh ....
[HttpPut]
[HttpPatch]
async Task meh ....
Angius
Angius3mo ago
Still, the user knows what they called If they called client.PostAsJsonAsync() they called a POST method No need to tell them "ah, yes, this was indeed a post request"
Sun「無用」
Sun「無用」OP3mo ago
also I like problem response and it has it there :d
Angius
Angius3mo ago
ProblemDetails does not return a request method
Sun「無用」
Sun「無用」OP3mo ago
ProblemDetails doesn't work properly for me and it just looks incomplete, so I just did a
public record struct ProblemResponse<T>(
HttpStatusCode StatusCode,
string Title,
string Method,
string Path,
T? Data,
ApplicationError[] Errors
);
public record struct ProblemResponse<T>(
HttpStatusCode StatusCode,
string Title,
string Method,
string Path,
T? Data,
ApplicationError[] Errors
);
Angius
Angius3mo ago
Status code that gets returned is the status code that gets returned...? Why even include that The method of the request is the method of the request
Pobiega
Pobiega3mo ago
[HttpGet("anon")]
public ActionResult<MyHttpDto> AnonymousEndpoint()
{
var method = HttpContext.Request.Method;
var path = HttpContext.Request.Path.ToString();

return new MyHttpDto(method, path);
}
[HttpGet("anon")]
public ActionResult<MyHttpDto> AnonymousEndpoint()
{
var method = HttpContext.Request.Method;
var path = HttpContext.Request.Path.ToString();

return new MyHttpDto(method, path);
}
https://localhost:7089/test/anon
{
"method": "GET",
"path": "/test/anon"
}
{
"method": "GET",
"path": "/test/anon"
}
Sun「無用」
Sun「無用」OP3mo ago
I've seen way too much code with people checking for status in the json, so I just do that by default now, lol
Angius
Angius3mo ago
You call GET /foo/bar, get a 401 response with an error that says "you called /foo/bar with GET method and got a 401"
Pobiega
Pobiega3mo ago
its all right there.
Angius
Angius3mo ago
Makes zero sense to me No need to cater to half-brained bumbling idiots Fuck them seven ways to sunday and tell them to learn how to do shit properly I never cared about the 20 IQ Cro-Magnons trying to use my shit I recommend you don't either It's easier and more sane this way
Sun「無用」
Sun「無用」OP3mo ago
thx
Angius
Angius3mo ago
If they get a response with HTTP status code 403 and then wonder "uhhhh ugg got 503 but what the status code, ugg no get this" that's their problem
Sun「無用」
Sun「無用」OP3mo ago
also btw, status is a property in the Problem RFC
No description
Angius
Angius3mo ago
I guess it does cater to the forementioned underdeveloped amoeabs, then Even it doesn't contain the request method and path, though
Sun「無用」
Sun「無用」OP3mo ago
It has instance, which could be just the full path
Angius
Angius3mo ago
Still not the method at least
Sun「無用」
Sun「無用」OP3mo ago
welp, if I'm giving the status/path, then I should prolly complete the info, lol
Want results from more Discord servers?
Add your server