gpt4 book ai didi

c# - ASP.Net Core Web API 应用程序 : How to return success from all my action methods and not run the code in a certain mode?

转载 作者:行者123 更新时间:2023-11-30 21:40:52 24 4
gpt4 key购买 nike

我正在以各种模式配置我的服务器,我正在从一个配置文件中设置应用程序的模式。所以,如果我运行我的 HTTP 服务器处于模式“X”,我希望客户端在到达有效端点时获得“HTTP STATUS 200 并且不执行任何逻辑”。如果服务器处于“Y”模式,则所有端点都应执行逻辑并根据请求处理返回状态

[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();
}

}

有没有一种使用过滤器的方法可以在不进入操作方法的情况下向客户端返回 OK?

编辑:当我说模式时,我的意思是像“生产”、“测试”、“登台”这样的模式。[就像我们对所有这些模式都有不同的数据库连接字符串]因此,我有一个名为“X”的模式 [如果我的服务器在模式“X”下运行],任何访问我公开的端点的客户端都会成功。

最佳答案

是的,您可以通过使用验证来做到这一点 middleware .我不明白您在 X 模式或 Y 模式下运行 HTTP 服务器的确切含义,因此您需要调整代码。

例如,请看下图:

enter image description here

您需要实现图中的“授权中间件”之类的东西。如果在 Controller 上(Startup.cs 中的 app.UseMvc()),您只需返回带有 HTTP 200 OK 的请求。

关于c# - ASP.Net Core Web API 应用程序 : How to return success from all my action methods and not run the code in a certain mode?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44117964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com