C
C#2y ago
Anton

❔ 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:
app.UseEndpoints(endpoints =>
{
if (app.Environment.IsDevelopment())
{
endpoints.Map("whatever", () => "blah");
}
});
app.UseEndpoints(endpoints =>
{
if (app.Environment.IsDevelopment())
{
endpoints.Map("whatever", () => "blah");
}
});
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:
endpoints.Map("/{**catchAll}", async (HttpContext http, string catchAll) => "whatever");
endpoints.Map("/{**catchAll}", async (HttpContext http, string catchAll) => "whatever");
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
Anton
Anton2y ago
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.
plam
plam2y ago
As far as I am concerned, it is actually possible via using IHostEnvironment inside your controller class, and basically checking for the same thing:
if (_hostEnvironment.IsDevelopment()) {..}
if (_hostEnvironment.IsDevelopment()) {..}
Additionally, you're actually able to implement a filter factory interface just to use [DevOnly] attribute before your controller class.
Anton
Anton2y ago
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
plam
plam2y ago
After the successful injection of the interface you're pretty much able to check for the environment inside of the body of action.
public class ExampleController : Controller
{
private readonly IHostEnvironment _hostEnvironment;

public ExampleController(IHostEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
}

public IActionResult View()
{
if (!_hostEnvironment.IsDevelopment()) // Return something else for Development.
}
}
public class ExampleController : Controller
{
private readonly IHostEnvironment _hostEnvironment;

public ExampleController(IHostEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
}

public IActionResult View()
{
if (!_hostEnvironment.IsDevelopment()) // Return something else for Development.
}
}
Anton
Anton2y ago
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
plam
plam2y ago
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...
plam
plam2y ago
There's an answer with an implementation of a filter, go for it It depends
Anton
Anton2y ago
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 tho
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Anton
Anton2y ago
Not debug There should be a separate PRODUCTION that I would define in a production build
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Anton
Anton2y ago
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
plam
plam2y ago
Why don't you just return NotFound as for the controller which is flagged with attribute for DevOnly or sum
Anton
Anton2y ago
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
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server
More Posts