user1179299
user1179299

Reputation: 376

ASP.Net Core Web API application : How to return success from all my action methods and not run the code in a certain mode?

I am configuring my server in various modes and I am setting the modes of the application from a config file. So, say if I run my HTTP server is in mode "X", I want clients to get "HTTP STATUS 200 and don't execute any logic" if they hit a valid endpoint. and if the server is in mode "Y" all endpoints should perform the logic and return the status as per the request processing

[Route("api/todo")]
public class TodoController : Controller{

        //if in mode "Y"
        [HttpGet("{id}", Name = "GetTodo")]
        public IActionResult GetById(long id)
        {
            var item = _todoRepository.Find(id);
            if (item == null)
            {
                return NotFound();
            }
            return new ObjectResult(item);
        }

       //if in mode "X"
       [HttpGet("{id}", Name = "GetTodo")]
       public IActionResult GetById(long id){
         return Ok();
       }

}

Is there a way using filter that can return OK to the client without getting inside of the action method?

EDIT: When i say modes, I mean like modes like "production", "testing", "staging". [like we have different db connection strings for all these modes] So, I have a mode called "X" [and if my server is running on mode "X"], any client who hits the endpoints exposed by me will get success.

Upvotes: 3

Views: 2734

Answers (1)

Guilherme
Guilherme

Reputation: 5351

Yes, you can do this by using a validation middleware. I don't get exactly what you mean by running the HTTP server in mode X or mode Y, so you need to adapt your code.

For example, take a look at the picture below:

enter image description here

You'll need to implement something like the "Authorization middleware" in the picture. You just need to return the request with HTTP 200 OK before if gets on the controllers (app.UseMvc() in the Startup.cs).

Upvotes: 3

Related Questions