Return JsonResult from web api without its properties

前端 未结 4 2093
日久生厌
日久生厌 2021-01-31 10:34

I have a Web API controller and from there I\'m returning an object as JSON from an action.

I\'m doing that like this:

public ActionResult GetAllNotifica         


        
4条回答
  •  灰色年华
    2021-01-31 11:14

    When using WebAPI, you should just return the Object rather than specifically returning Json, as the API will either return JSON or XML depending on the request.

    I am not sure why your WebAPI is returning an ActionResult, but I would change the code to something like;

    public IEnumerable GetAllNotificationSettings()
    {
        var result = new List();
        // Filling the list with data here...
    
        // Then I return the list
        return result;
    }
    

    This will result in JSON if you are calling it from some AJAX code.

    P.S WebAPI is supposed to be RESTful, so your Controller should be called ListItemController and your Method should just be called Get. But that is for another day.

提交回复
热议问题