1

I want to exclude a controller from .NET Core Web Api, because it's not ready for production.

I know we can add an attribute, but I am trying to do it runtime depending on the environment from the Startup class.

Is there a way to do it when we register the controllers?

Thank you

1 Answer 1

3

According to your description, I suggest you could try to write a custom middleware to achieve your requirement.

At this middleware we could check the request path if this request path contains the path and the environment is the the development then we could directly return the 404.

app.Use(async (context, next) =>
{
    if (env.IsDevelopment()
        && context.Request.Path.Value.Contains("WeatherForecast"))
    {
        context.Response.StatusCode = 404;
        return;
    }
    else
    {
        await next.Invoke();
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.