简体   繁体   中英

Should I use IActionResult in a get request that returns a json object

I have an ASP.NET API that goes as following

[HttpGet]
[Route("GetModel")]
public async Model GetModel(string modelId)
{
    return _service.GetModel(modelId);
}

and a client that goes as following

public async Model GetModel(string modelId)
{
    var uri = $"api/Model/GetModel?modelId={modelId}";
    return await _httpClient.GetJsonAsync<Model>(uri);
}

I am not using IActionResult or status codes. I am also not doing any exception handling in the API. (I am just taking appropriate action on the client if the model is returned null.)

Do I need to use IActionResult or status codes? What is the best practice for this? This is a high-level question so I would appreciate it if you were to elaborate on the concepts.

You can read about it here.: https://www.learnrazorpages.com/razor-pages/action-results this explains in great detail as to why and what best practices are.

If your handler method returns more than one type of action result depending on conditions, you will need to broaden the return type of the method to ActionResult or IActionResult.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM