How to return Json object on Web API Controller

前端 未结 3 1452
慢半拍i
慢半拍i 2020-12-23 15:06

I used this below code on my asp.net controller to return Json object on my Ajax on javascript

public JsonResult myMethod()
{
    // return a Json Object, yo         


        
3条回答
  •  执笔经年
    2020-12-23 15:20

    ASP.NET Web API works with a little bit different philosophy. You should return just an entity (or set of entities) and it is up to content negotiation mechanism to return it to the client in the format which he has requested. You can read more about content negotiation here:

    • Everything you want to know about ASP.NET Web API content negotiation

    You can of course bypass the content negiotiation by returning a HttpResponseMessage. In this case yo need to serialize the object into JSON yourself (basics of this approach are also described in the article mentioned above).

提交回复
热议问题