Helping you always. Don't forget to click "Mark as Answer" on the post that helped you.
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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.
Member
273 Points
290 Posts
JsonResult or ActionResult
Mar 21, 2019 09:18 PM|rkrex|LINK
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 !
All-Star
52231 Points
23303 Posts
Re: JsonResult or ActionResult
Mar 21, 2019 09:41 PM|mgebhard|LINK
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.
Participant
1253 Points
930 Posts
Re: JsonResult or ActionResult
Mar 22, 2019 05:33 AM|yogyogi|LINK
You can return an object from the API and this object will be returned in JSON format:
You can also return HTTP status from the API like:
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
♠ ASP.NET Core Tutorials → Start from the Beginning and become an Expert in 30 days time ♠
Contributor
3710 Points
1431 Posts
Re: JsonResult or ActionResult
Mar 22, 2019 06:57 AM|Yuki Tao|LINK
Hi rkrex,
A Web API controller action can return any of the following:
I suggest you could use IHttpActionResult rather than JsonResult and ActionResult.
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:
Controller:
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
All-Star
57874 Points
15510 Posts
Re: JsonResult or ActionResult
Mar 22, 2019 02:51 PM|bruce (sqlwork.com)|LINK
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.