locked
JsonResult or ActionResult RRS feed

  • Question

  • User-305496339 posted

    Hi Friends,

    Should a Web API return a JsonResult or a ActionResult? Also I was wondering should I use Jquery Ajax with my Web API's? Thanks !

    Thursday, March 21, 2019 9:18 PM

Answers

  • User475983607 posted

    Should a Web API return a JsonResult or a ActionResult? Also I was wondering should I use Jquery Ajax with my Web API's? Thanks !

    Please take the time read through the Getting Started tutorials as it covers the basics. 

    Generally, Web API returns an object and/or HTTP status.   Using AJAX or not is up to you and your project requirements.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 21, 2019 9:41 PM
  • User-2054057000 posted

    You can return an object from the API and this object will be returned in JSON format:

    public Student Get(int id) {
        Student stu = new Student();
        stu.Name = "Jack";
        stu.Age = 10;
        return stu; 
    }

    You can also return  HTTP status from the API like:

    public StatusCodeResult Get(int id) {
        if (id != null)
        {
            // do some work
            return Ok();
        }
        return NotFound();
    }

    Tutorial - API Controllers – Creating API in ASP.NET Core

    Your other question is regarding jQuery AJAX. The jQuery AJAX can be used by the client to consume the API.

    See - How to use JQuery AJAX Method to Call an API

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 22, 2019 5:33 AM
  • User1520731567 posted

    Hi rkrex,

    Should a Web API return a JsonResult or a ActionResult?

    A Web API controller action can return any of the following:

    1. void
    2. HttpResponseMessage
    3. IHttpActionResult
    4. Some other type

    I suggest you could use IHttpActionResult rather than JsonResult and ActionResult.

    Also I was wondering should I use Jquery Ajax with my Web API's?

    You could use Jquery Ajax or HttpClient or any other to call Web API,It depends on you.

    For example:

    1.Call web api by ajax:

     $.ajax({
                        type: "POST",
                        url: "/api/AjaxAPI/AjaxMethod",
                        data: person,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            alert("Hello: " + response.Name + ".\nCurrent Date and Time: " + response.DateTime);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        },
                        error: function (response) {
                            alert(response.responseText);
                        }
                    });

    Controller:

    public class AjaxAPIController : ApiController
    {
        [Route("api/AjaxAPI/AjaxMethod")]
        [HttpPost]
        public PersonModel AjaxMethod(PersonModel person)
        {
            person.DateTime = DateTime.Now.ToString();
            return person;
        }
    }

    More details about Call (Consume) Web API using jQuery AJAX in ASP.Net MVC,you could refer to:

    https://www.aspsnippets.com/Articles/Call-Consume-Web-API-using-jQuery-AJAX-in-ASPNet-MVC.aspx

    2.Call web api by HttpClient:

    More details about Consume Web API in .NET using HttpClient,you could refer to:

    https://www.tutorialsteacher.com/webapi/action-method-return-type-in-web-api

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 22, 2019 6:57 AM
  • User-474980206 posted

    1) if you just return an object from webapi, it will support the accept type sent in the request. so the api can return json or xml. 

    2) while you can use jQuery, the native fetch api is more powerful, and preferred. unless you need support for IE < 11, there really is little reason for jQuery anymore.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 22, 2019 2:51 PM

All replies

  • User475983607 posted

    Should a Web API return a JsonResult or a ActionResult? Also I was wondering should I use Jquery Ajax with my Web API's? Thanks !

    Please take the time read through the Getting Started tutorials as it covers the basics. 

    Generally, Web API returns an object and/or HTTP status.   Using AJAX or not is up to you and your project requirements.  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, March 21, 2019 9:41 PM
  • User-2054057000 posted

    You can return an object from the API and this object will be returned in JSON format:

    public Student Get(int id) {
        Student stu = new Student();
        stu.Name = "Jack";
        stu.Age = 10;
        return stu; 
    }

    You can also return  HTTP status from the API like:

    public StatusCodeResult Get(int id) {
        if (id != null)
        {
            // do some work
            return Ok();
        }
        return NotFound();
    }

    Tutorial - API Controllers – Creating API in ASP.NET Core

    Your other question is regarding jQuery AJAX. The jQuery AJAX can be used by the client to consume the API.

    See - How to use JQuery AJAX Method to Call an API

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 22, 2019 5:33 AM
  • User1520731567 posted

    Hi rkrex,

    Should a Web API return a JsonResult or a ActionResult?

    A Web API controller action can return any of the following:

    1. void
    2. HttpResponseMessage
    3. IHttpActionResult
    4. Some other type

    I suggest you could use IHttpActionResult rather than JsonResult and ActionResult.

    Also I was wondering should I use Jquery Ajax with my Web API's?

    You could use Jquery Ajax or HttpClient or any other to call Web API,It depends on you.

    For example:

    1.Call web api by ajax:

     $.ajax({
                        type: "POST",
                        url: "/api/AjaxAPI/AjaxMethod",
                        data: person,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            alert("Hello: " + response.Name + ".\nCurrent Date and Time: " + response.DateTime);
                        },
                        failure: function (response) {
                            alert(response.responseText);
                        },
                        error: function (response) {
                            alert(response.responseText);
                        }
                    });

    Controller:

    public class AjaxAPIController : ApiController
    {
        [Route("api/AjaxAPI/AjaxMethod")]
        [HttpPost]
        public PersonModel AjaxMethod(PersonModel person)
        {
            person.DateTime = DateTime.Now.ToString();
            return person;
        }
    }

    More details about Call (Consume) Web API using jQuery AJAX in ASP.Net MVC,you could refer to:

    https://www.aspsnippets.com/Articles/Call-Consume-Web-API-using-jQuery-AJAX-in-ASPNet-MVC.aspx

    2.Call web api by HttpClient:

    More details about Consume Web API in .NET using HttpClient,you could refer to:

    https://www.tutorialsteacher.com/webapi/action-method-return-type-in-web-api

    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 22, 2019 6:57 AM
  • User-474980206 posted

    1) if you just return an object from webapi, it will support the accept type sent in the request. so the api can return json or xml. 

    2) while you can use jQuery, the native fetch api is more powerful, and preferred. unless you need support for IE < 11, there really is little reason for jQuery anymore.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 22, 2019 2:51 PM