【问题标题】:.net core 2.2 Web Api not returning any messages, Status code returned fine.net core 2.2 Web Api没有返回任何消息,状态码返回正常
【发布时间】:2020-01-18 21:40:54
【问题描述】:

我正在使用 v2.2 设置一个新的 .net core webapi。 这有一个简单的 get 函数,如果请求是心跳或 Json 对象,它应该返回当前日期。

我已使用 IActionResult 返回类型将心跳函数设置为 HTTPGet。 我已经尝试过同时使用 Controllerbase 类和 Controller 类,但无论我尝试哪一个都会遇到同样的问题

我的控制器类声明如下:

public class TestController : ControllerBase
{
    IConfiguration _iconfiguration;


    public TestController(IConfiguration configuration)
    {
        _iconfiguration = configuration;
    }

    // GET: api/Interceptor        
    [HttpGet("Heartbeat")]        
    public IActionResult Get()
    {


        //Set the return DateTime for return value
        string currentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");            

        return Ok(currentDateTime);
    //return Ok("Heartbeat Message Received");
        //return Ok(new { message = "Heartbeat Message Received: " + currentDateTime });            
        //var result = new OkObjectResult(new { message = "200 OK", currentDate = DateTime.Now });
        //return result;
       //return Ok(Json("123"));            
        //return new string[] { "value1", "value2" };
    }
}

如您所见,我尝试了各种方法来尝试获取要返回的返回字符串。 当我在 Postman 中对该服务进行调用时,我得到了 200Ok 返回状态,但是我的响应主体(在这种情况下为当前日期时间)只是返回为空。 尝试的返回类型:

  • 好的(结果)
  • 好的(字符串)
  • OkResultObject(对象)
  • 字符串
  • 数组

在所有情况下,Postman 都会返回状态 200 Ok 但没有正文。

Screenshot of what I see when I do a postman call

我使用的IDE是VS2017 Enterprise version 15.9.7

【问题讨论】:

  • 如果您在浏览器中粘贴 GET 请求的 url,您会看到正文吗?
  • @KrishnaDhugana 不,我在浏览器中的 Get 请求也给了我相同的结果
  • IActionResult 不是都与旧的 MVC 5.2 一起消失了,您现在应该返回对象吗?
  • 你怎么能确定它没有返回正文中的值,而只是返回 200 成功代码?能发个截图吗?
  • @Thangadurai 我附上了我在拨打邮递员电话时得到的结果的屏幕截图。如您所见,它返回 HTTP 状态代码,但不是我要传回的结果

标签: c# .net asp.net-core-webapi


【解决方案1】:

你可以尝试在你的控制器类上使用这个属性

[Route("api/[controller]")]
[ApiController]
public class MyCustomController : ControllerBase

您可以在https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2阅读更多内容

【讨论】:

  • 谢谢,但我已经尝试过 Controller 和 ControllerBase。两者都给了我相同的返回状态代码问题,但没有正文。我也尝试了上面的链接,但没有看到任何对我有帮助的东西
【解决方案2】:

你需要使用你的类的属性:

[Route("api/[controller]")]
[ApiController]

我创建了一个新项目来测试这个,我开始调试模式并调用了我的地址:

http://localhost:my_port/api/Test

我的 API 的地址是 Test,但我的类是 TestController。

我的课很简单:

[Route("api/[controller]")]
public class TestController : Controller
{
    // GET: api/<controller>
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

}

然后返回:["value1","value2"]

试试这个。

【讨论】:

  • 我的路由和 API 控制器属性已经设置好了。感谢您提供此示例,但它适用于 VS IDE 设置的默认控制器。这对我也很好。但是,当我将 IEnumerable 更改为 IActionResult 时,我遇到了问题
【解决方案3】:

此操作的路线是/Heartbeat,因为这是您设置的。不确定/V1/Interceptor/Heartbeat 击中的是什么,但不是这个动作。如果这是您想要执行此操作的路线,那么您需要将以下属性添加到您的控制器:

`[Route("V1/Interceptor")]`

但是,鉴于您目前没有获得 404,似乎有一些东西已经在使用它,所以当您这样做时,您可能会遇到路由冲突。

【讨论】:

    【解决方案4】:
    public class TestController : ControllerBase
    {
        IConfiguration _iconfiguration;
    
    
        public TestController(IConfiguration configuration)
        {
            _iconfiguration = configuration;
        }
    
        // GET: api/Interceptor        
        [HttpGet("Heartbeat")]        
        public IActionResult Get()
           {
                //Set the return DateTime for return value
                string currentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
                return Ok(new
                {
                    data = currentDateTime
                });
            }
    }
    
    
    //By this you will get the body parameter as well as you can return many more properties with this like status_code, message etc..
    

    【讨论】:

    • 谢谢。我已经尝试过了,但它仍然没有返回 currentdate 变量
    • @rbharany1 它的工作在这里,可能是您需要更正路由路径。在我的本地机器中,URL:localhost:44315/api/test/heartbeat 并得到像 data : "2019-09-18 10:33:06:151" 这样的响应,所以请您验证一下URL,以便您可以根据需要获得准确的输出。
    • 由于这是 .NET 核心(c# 应用程序),我无法使其生效并与您分享确切的输出。所以我需要在这里发表评论,以便您可以解决您的问题。
    猜你喜欢
    • 2020-09-07
    • 1970-01-01
    • 2023-01-12
    • 2020-08-05
    • 2017-06-09
    • 2016-07-11
    • 2020-11-10
    • 1970-01-01
    • 2019-08-11
    相关资源
    最近更新 更多