ASP.Net IActionResult always returns Ok
I have the following method inside of a service, I call this method from a controller
Where its suppsoe to return 404 not found it enters the return statement and jumps to the last line of the method (Return Ok)
This seems to happen on all methods, possibly there is a better way of returning values from services?
This is how I call this method
40 Replies
Are you sure the condition is even being called?
You should print out your condition and set a print statement into the error result so you know the state of the condition and if it's being called
The condition is being called, its not that, it enters the return statement then the return statement makes it jump to the bottom fo the method making it reutrn 200 instead of 404
return NotFoundResult instead of NotFoundObjectResult
maybe that works
It probably "enters" the if because it needs to execute the db call and get the server, meaning it exists
I'd extract the condition to a separate variable, just to see what happens
And to be able to place a breakpoint on it to see what it actually is
(also, why
DbContextFactory
...?)To keep a seperate context no? altough I assume DI would take care of that, also, thats not whaat I meant, what I meant is it will enter the if and it should return 404
but instead it jumps to the bottom of the code
It is something that literally makes 0 sense logically
that return makes it jump there
Just inject the context, save yourself the trouble
instead of actually returning from the method
Huh
Yeah, that does make no sense
Yeah
I'd still try to extract the condition and see what it is
been trying not to smash my head against a wall
Nah the condition is fine, its cant find the id like its suppose to fail there cuz im feeding it false data
but it should return 404
this happens on every method
Mind you this could be something to do with the fact that this method isnt in a controller but in service
I just wonder whether it's
await (FindFirst is not {} server)
or (await FindFirst) is not {} server
that happens
Because FindFirstAsync
would always return a Task
, which would mean it technically never matches the conditionasync concurrency issue possibly
Unlikely, you're not doing any super complex code here
More likely that it's pattern matching on a task, not on its result
hmm wdym by that
actually
let me convert it to a normal if then
@ZZZZZZZZZZZZZZZZZZZZZZZZZ You mind if i dm you a vid of this happening? cuz it has a bunch of data thats relativly sensitive and its hard for me to show you exactly what is happening without a vid
What I mean, is
might be treated as
and not
yes but the return statement still shouldnt skip to the bottom
Let's remove anything that could possibly be an issue first, aight?
btw
I guess I can open my DMs briefly
alright 1m then
There
just fyi the src looks a bit diff to what i sent in-order to protect privacy but yeah
1s
just var name changes
if yw close them i ll let you know when im gonna send
gotta record it rq
I think i understand why
fucking hell
The method that calls this was a sync method and this is async, I also didnt return the value of this call properly just returned Ok
so I assume what happened is because this is async it skipped over it and returned Ok which is why it did this
Ah, well, async over sync will do that to you lmao
still weird as hell to see that
Yeah, if something's async but unawaited, the execution will just skip over it
I take it I can close my DMs now?
Yes sure
Yeah that seemed to be the issue, weird as hell but yeah still thanks for the help
I wonder if there's some "unawaited task" analyzer
debugger at most ig
but its hard to do that because at the same time, it has to intended design
there is _ = asyncMethod() to ignore
other wise it screams with a warning
but thats about it
Yeah
Never wrote a Roslyn analyzer, but I might add this to my growing list of future projects lol
Sounds quite cool id be interested to join you on that down the line
Never worked too much with compilers tho
Generators and analyzers aren't the most ergonomic to write, but they're not that bad
I can recommend it for a weekend project
worked a bit with src gens via attributes
wanted to blow my brains out
I wanted to create a macro language similar to rusts its funny because its very possible in c# just nobody has done it
i.e
then have a src gen generate a for loop to repeat a print 5 times
just its annoying as hell, writing a macro to c# transpiler
Sounds like a job for interceptors
then dealing with src gen
But yeah, this would basically require an entire DSL embedded in the generator lol
altough rust managed to do it
surprised no one bothered in c#
:SCcrying: