How to return Json object on Web API Controller

前端 未结 3 1455
慢半拍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条回答
  •  梦毁少年i
    2020-12-23 15:31

    After reading tpeczek's answer, Darrel Miller's answer, and their comment conversation in tpeczek's answer, I wanted get more guidance about when or why I might want to use Web Api and its content negotiation mechanism. tpeczek's link is informative and useful, but I found a couple other write-ups that were more geared at comparing the use of Web Api (and its content negotiation) with, say, plain MVC 4 controller actions that return JsonResult. Here are the ones that I found useful to making such a decision. One of the author concludes that he prefers using plain MVC 4 controllers while the other author prefers using Web Api controllers:

    Building a Public HTTP API for Data

    I believe there is one correction needed in the above author's post. In there he mentions that,

    "...every [Controller] method that begins with 'Get' is automatically associated to the GET verb. Does it sound great? It is, but it also means that you can’t have two methods whose name begin with 'Get' in the same controller class."

    According to this answer, you can indeed have multiple 'Get' methods in the same controller if you specify an ActionName attribute. Now here's the second post:

    ASP.NET Web API vs. ASP.NET MVC “APIs”

提交回复
热议问题