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
30 Replies
Not sure what you mean
The method will be
GET
, as the attribute says
The path will be what you define thereyes, but it's not in the code, I can't use it as a variable or return it to the user
yeah just look at
HttpContext
you dont need to inject it, its a property on the controllerbaseLike that
U can use nameof()
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 pathI'm lost here, lol
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
pathbc I just want the info, most users are kinda dumb :d
I've no clue why you'd want it
its all in HttpContext
It's like sending an email to
[email protected]
and getting a reply "you sent an email to [email protected]"also I'm gonna do stuff like
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"also I like problem response and it has it there :d
ProblemDetails
does not return a request methodProblemDetails doesn't work properly for me and it just looks incomplete, so I just did a
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
https://localhost:7089/test/anon
I've seen way too much code with people checking for status in the json, so I just do that by default now, lol
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"its all right there.
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
thx
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
also btw, status is a property in the Problem RFC
I guess it does cater to the forementioned underdeveloped amoeabs, then
Even it doesn't contain the request method and path, though
It has instance, which could be just the full path
Still not the method at least
welp, if I'm giving the status/path, then I should prolly complete the info, lol