1
votes

I am setting up a new .net core webapi using v2.2. This has a simple get function which is supposed to return either the current date if the request is a heartbeat or a Json object.

I have set up the heartbeat function as a HTTPGet using an IActionResult return type. I have tried using both the Controllerbase class as well as the Controller class but am having the same issue no matter which I try

My controller class declaration is as follows:

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" };
    }
}

As you can see I have tried various methods to try and get a return string to be delivered back. When I do the call in Postman to this service, I get the 200Ok return status fine, however the body of my response (in this case the current date time) just comes back empty. return types tried:

  • Ok(result)
  • Ok(string)
  • OkResultObject(object)
  • string
  • array

In all cases Postman returns me a status 200 Ok but no body.

Screenshot of what I see when I do a postman call

The IDE I am using is VS2017 Enterprise version 15.9.7

4
If you paste url for your GET request in browser, do you see body? - KrishnaDhungana
@KrishnaDhugana No my Get request in a browser is also giving me the same result - rbharany1
Isn't IActionResult all gone with the old MVC 5.2 and you should just return objects now? - zaitsman
How can you be sure that it is not returning the value in the body but only the 200 success code? Can you post a screen shot. - Thangadurai
@Thangadurai I have attached a screenshot of the result I get while doing a Postman call. As you will see it returns the HTTP Status code but not the result I want to pass back - rbharany1

4 Answers

1
votes

You can try use this attribute on your controller class

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

You can read more on https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2

1
votes

You need use the attributes on your class:

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

I created a new project for test this, i started on debug mode and called my address:

http://localhost:my_port/api/Test

The address my API is Test, but my class called TestController.

My class is very simple:

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

}

And returns: ["value1","value2"]

Try this.

1
votes

The route for this action is /Heartbeat, because that's what you've set it to. Not sure what /V1/Interceptor/Heartbeat is hitting, but it's not this action. If that's the route you want for this action then you need to add the following attribute to your controller:

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

However, given that you're not getting a 404 currently, there's something that's already using that, it would seem, so you're likely get a route conflict when you do that.

0
votes
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..