❔ ASP.NET Core how to let a controller be mapped only in development mode
This is pretty easy to do with minimal API's, you just do a check and map only if it succeeds:
However, as I've noticed, minimal endpoints have somewhat different semantics than normal controller actions. For example, they can't match catch all routes. So the following won't work:
With controllers I think it does work.
Minimal API's specificity is quite weird too. They'd match a request even having a less specific route than some of the controllers. It's as though they're treated completely separately, idk.
15 Replies
To be clear, I don't want to make a filter and then check if it's development mode in the filter of the controller, or whatever. I don't want it to be mapped at all if it's not development.
As far as I am concerned, it is actually possible via using IHostEnvironment inside your controller class, and basically checking for the same thing:
Additionally, you're actually able to implement a filter factory interface just to use [DevOnly] attribute before your controller class.
can you elaborate?
would I throw in the constructor or something? I don't understand
can filters run prior to mapping even?
or like while resolving what to map
yeah I've seen this post, I'm confused tho
After the successful injection of the interface you're pretty much able to check for the environment inside of the body of action.
would the filter in the second answer run once prior to the mapping being established, or every time
no this is not what I want
like I said, I don't want it to be mapped at all if it's development
this would still map it
Stack Overflow
Conditionally disable ASP.NET MVC Controller
What is the best way to disable ASP.NET MVC controller conditionally?
I want to have an access to the controller actions if some value in web.config is "true" and 404 if it's "false"
Should I wr...
There's an answer with an implementation of a filter, go for it
It depends
This would still map the controller and its actions, I really don't want that
Like I'm just tempted to put a
#if DEVELOPMENT
or something on the class
but I see that people don't usually do that in ASP.NET Core
Don't know why thoUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
Not debug
There should be a separate
PRODUCTION
that I would define in a production buildUnknown User•3y ago
Message Not Public
Sign In & Join Server To View
debug vs production should be two separate concepts
that's not what I mean
I mean you may still want to build for prodcution in debug mode
for testing purposes
It's fine, I have a Nuke task doing a production build
Why don't you just return NotFound as for the controller which is flagged with attribute for DevOnly or sum
because it's a catch all controller
I may want to have some other logic for that case, defined somewhere else
+ it's extra work done for no reason
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.