C# 从不工作的WebApi返回JsonResult ASP.NET

C# 从不工作的WebApi返回JsonResult ASP.NET,c#,json,asp.net-mvc,asp.net-web-api,jsonresult,C#,Json,Asp.net Mvc,Asp.net Web Api,Jsonresult,我有一个MVC项目。我想得到一个简单的json响应{result:“ok”}。下面是我的代码 using System; using System.Web.Mvc; using Microsoft.Xrm.Sdk; using CRM_WebApp.Models; using System.Web.Http; using System.Web.Http.Cors; using Microsoft.Xrm.Sdk.Query; using CRM_WebApp.Services; namespa

我有一个MVC项目。我想得到一个简单的json响应{result:“ok”}。下面是我的代码

using System;
using System.Web.Mvc;
using Microsoft.Xrm.Sdk;
using CRM_WebApp.Models;
using System.Web.Http;
using System.Web.Http.Cors;
using Microsoft.Xrm.Sdk.Query;
using CRM_WebApp.Services;

namespace CRM_WebApp.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class CallBackFormController : ApiController
    {

        [System.Web.Mvc.HttpPost]
        public JsonResult Post([FromBody] CallBackFormModel CallBackFormModel)
        {
            ConnectiontoCrm connectiontoCrm = new ConnectiontoCrm();
            //connectiontoCrm.GetConnectiontoCrm();
            connectiontoCrm.GetConnectiontoCrmCopy();

            Entity lead = new Entity("lead");
            lead["firstname"] = CallBackFormModel.FirstName;
            lead["mobilephone"] = CallBackFormModel.Telephone;
            lead["telephone3"] = CallBackFormModel.Telephone;

            Guid tisa_callbackformid = connectiontoCrm.organizationservice.Create(callbackform);
            return new JsonResult { Data = new { result = "ok" } };
        }
    }
}
我的代码给出了以下响应:

{
    "ContentEncoding": null,
    "ContentType": null,
    "Data": {
        "result": "ok"
    },
    "JsonRequestBehavior": 1,
    "MaxJsonLength": null,
    "RecursionLimit": null
}
如何更改代码以获得响应:{result:“ok”}

尝试以下操作:

return Json(new { result = "ok" }, JsonRequestBehavior.AllowGet);

在对您的代码进行了一些调查之后,我真的注意到存在一些基本错误

  • 当您从
    ApiController
    继承时,这里您创建的是WebApiController,而不是MVC控制器(可以通过从
    controller
    类继承来创建)

  • 您必须小心使用的名称空间,因为有些类和属性具有相同的名称,但在不同的名称空间中,例如,
    HttpPost
    属性存在于
    System.Web.Http
    System.Web.Mvc
    中,根据您的代码,您必须使用前一个命名空间中的属性,因为您是从
    ApiController
    继承的

  • 请记住,
    System.Web.Mvc
    用于ASP.NET Mvc,
    System.Web.Http
    用于Web API

  • 您没有为方法(表示Web API方法)使用正确的返回类型
  • 所以在解决了之前的所有问题之后,工作代码应该是这样的

    [System.Web.Http.HttpPost]
    public System.Web.Http.IHttpActionResult Post([System.Web.Http.FromBody] CallBackFormModel CallBackFormModel)
    {
        // your previous code goes here
        return Json(new { result = "ok" }, JsonRequestBehavior.AllowGet);
    }
    

    我建议您阅读ASP.NET MVC和Web API以及它们之间的差异,以避免将来出现此类问题

    为了回答您的问题,我遇到了同样的问题,因为当我返回响应时,我喜欢返回多个对象/类型

    例如,我总是返回包含一些成功和错误消息的消息结果。这个对象在我的所有api响应中几乎都是重用的。然后,我想返回第二个对象,其中包含任何数据,例如客户列表或其他任何数据

    这就是我为WebAPI工作的方式

    public IHttpActionResult DeleteEmailTemplate(int id)
        {
            FormResponse formResponse = new FormResponse("SUCESS MESSAGE HERE");
    
            List<string> strings = new List<string>();
            strings.Add("this is a test");
            strings.Add("this is another test");
    
            return Json(new { MessageResult = formResponse, TestObject = strings });
        }
    
    public IHttpActionResult DeleteEmailTemplate(int-id)
    {
    FormResponse FormResponse=新的FormResponse(“此处成功消息”);
    列表字符串=新列表();
    添加(“这是一个测试”);
    Add(“这是另一个测试”);
    返回Json(新的{MessageResult=formResponse,TestObject=strings});
    }
    
    您的结果应该是
    {“result”:“ok”}
    因为
    {result:“ok”}
    无效,因为字符串应该用双引号括起来。然后,无论Json是什么,VisualStudio special copy past都允许您创建相应的类。但对于这样的一行json,我有点“矫枉过正”。我得到了一个错误:JsonResult不包含一个取1的构造函数。我得到错误:参数2:无法从System.Web.Mvc.JsonRequestBehavior转换为Newtonsoft.Json.JsonSerializerSettings
    return JsonResult(new{result=“ok”},JsonRequestBehavior.AllowGet)虽然此代码可以回答问题,但提供有关如何以及为什么解决问题的附加上下文将提高答案的长期价值。@OscarDominguezNavarrete我为其更新了答案you@HakamFostok我试过你的答案,但没有成功((我收到以下错误:无法隐式地将类型“System.Web.Http.Results.OkNegotiatedContentResult”转换为“System.Web.Mvc.JsonResult”