从MVC控制器中的JsonResult方法返回JSON

时间:2023-01-28 18:02:50

I am trying to populate a ComboBox (Telerik RAD COmboBox) in a test ASP.NET MVC3 app.

我试图在测试ASP.NET MVC3应用程序中填充ComboBox(Telerik RAD COmboBox)。

I have defined the ComboBox on my ASPX page and in the controller I have defined the action call that returns a JsonResult.

我在我的ASPX页面上定义了ComboBox,在控制器中我定义了返回JsonResult的动作调用。

The problem I am having is that the Web Service I am using already returns the resultset as a JSON string. How can I pass the response from the Webservice directly.

我遇到的问题是我使用的Web服务已经将结果集作为JSON字符串返回。如何直接从Webservice传递响应。

Here is the snippets of code: ASPX page:

以下是代码片段:ASPX页面:

<% Html.Telerik().ComboBox()
                       .Name("cbRefTables")
                       .DataBinding(b => b
                           .Ajax()
                           .Select("GetCALMdata","Common")                    
                       )
                       .Render();
                %>

Controller: called CommomController

控制器:称为CommomController

    public JsonResult GetCALMdata()
    {
        CALMwsP.wsCALMSoapClient wsC = new CALMwsP.wsCALMSoapClient("wsCALMSoap");
        string resultset = wsC.GetRefTables("P_1", "P_2", "P_3", "P_4");

        return ??; -- I want to return resultset which is already formatted. 
    }

4 个解决方案

#1


15  

If the resultset string is already JSON (and not wrapped in any XML), then you'd want to return a ContentResult with exactly that string as the content:

如果结果集字符串已经是JSON(并且没有包装在任何XML中),那么您需要返回一个ContentResult,其中包含该字符串作为内容:

public ContentResult GetCALMdata()
{
    CALMwsP.wsCALMSoapClient wsC = new CALMwsP.wsCALMSoapClient("wsCALMSoap");
    string resultset = wsC.GetRefTables("P_1", "P_2", "P_3", "P_4");

    return Content(resultset, "application/json");
}

You don't want to use JsonResult or the Json() helper in this case, because that's going to end up re-serializing your JSON.

在这种情况下,您不希望使用JsonResult或Json()帮助程序,因为这将最终重新序列化您的JSON。

#2


20  

If using ASP.NET MVC 2 or higher:

如果使用ASP.NET MVC 2或更高版本:

return Json(resultset, JsonRequestBehavior.AllowGet);

#3


3  

if I correctly understood you should use the Json() method

如果我正确理解你应该使用Json()方法

return Json(resultset);

#4


0  

The individual Json Method:

个人Json方法:

return Json(resultset);

return Json(resultset);

It needs the System.Web.Http DLL and the namespace is System.Web.Http.Results.

它需要System.Web.Http DLL,命名空间是System.Web.Http.Results。

从MVC控制器中的JsonResult方法返回JSON


Or Website wide put this line in the WebApiConfig.cs

或者网站范围将此行放在WebApiConfig.cs中

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

#1


15  

If the resultset string is already JSON (and not wrapped in any XML), then you'd want to return a ContentResult with exactly that string as the content:

如果结果集字符串已经是JSON(并且没有包装在任何XML中),那么您需要返回一个ContentResult,其中包含该字符串作为内容:

public ContentResult GetCALMdata()
{
    CALMwsP.wsCALMSoapClient wsC = new CALMwsP.wsCALMSoapClient("wsCALMSoap");
    string resultset = wsC.GetRefTables("P_1", "P_2", "P_3", "P_4");

    return Content(resultset, "application/json");
}

You don't want to use JsonResult or the Json() helper in this case, because that's going to end up re-serializing your JSON.

在这种情况下,您不希望使用JsonResult或Json()帮助程序,因为这将最终重新序列化您的JSON。

#2


20  

If using ASP.NET MVC 2 or higher:

如果使用ASP.NET MVC 2或更高版本:

return Json(resultset, JsonRequestBehavior.AllowGet);

#3


3  

if I correctly understood you should use the Json() method

如果我正确理解你应该使用Json()方法

return Json(resultset);

#4


0  

The individual Json Method:

个人Json方法:

return Json(resultset);

return Json(resultset);

It needs the System.Web.Http DLL and the namespace is System.Web.Http.Results.

它需要System.Web.Http DLL,命名空间是System.Web.Http.Results。

从MVC控制器中的JsonResult方法返回JSON


Or Website wide put this line in the WebApiConfig.cs

或者网站范围将此行放在WebApiConfig.cs中

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));