MVC:如何以JSONforms返回string

为了使进度报告过程更加可靠,并将其与请求/响应分离,我正在执行Windows服务中的处理,并将预期的响应持久化到文件。 当客户端开始轮询更新时,意图是控制器将文件的内容(不pipe它们是什么)作为JSONstring返回。

该文件的内容被预先序列化为JSON。 这是为了确保答复的方式没有任何障碍。 没有处理需要发生(短文阅读文件内容到一个string,并返回它)来获得响应。

我最初虽然这将是相当简单,但事实并非如此。

目前我的控制器方法看起来如此:

调节器

更新

[HttpPost] public JsonResult UpdateBatchSearchMembers() { string path = Properties.Settings.Default.ResponsePath; string returntext; if (!System.IO.File.Exists(path)) returntext = Properties.Settings.Default.EmptyBatchSearchUpdate; else returntext = System.IO.File.ReadAllText(path); return this.Json(returntext); } 

而提琴手正在返回这个原始的回应

 HTTP/1.1 200 OK Server: ASP.NET Development Server/10.0.0.0 Date: Mon, 19 Mar 2012 20:30:05 GMT X-AspNet-Version: 4.0.30319 X-AspNetMvc-Version: 3.0 Cache-Control: private Content-Type: application/json; charset=utf-8 Content-Length: 81 Connection: Close "{\"StopPolling\":false,\"BatchSearchProgressReports\":[],\"MemberStatuses\":[]}" 

AJAX

更新

以下可能会稍后改变,但现在这是工作时,我生成的响应类和返回像JSON一样的普通人。

 this.CheckForUpdate = function () { var parent = this; if (this.BatchSearchId != null && WorkflowState.SelectedSearchList != "") { showAjaxLoader = false; if (progressPending != true) { progressPending = true; $.ajax({ url: WorkflowState.UpdateBatchLink + "?SearchListID=" + WorkflowState.SelectedSearchList, type: 'POST', contentType: 'application/json; charset=utf-8', cache: false, success: function (data) { for (var i = 0; i < data.MemberStatuses.length; i++) { var response = data.MemberStatuses[i]; parent.UpdateCellStatus(response); } if (data.StopPolling = true) { parent.StopPullingForUpdates(); } showAjaxLoader = true; } }); progressPending = false; } } 

我相信这个问题是,Json操作的结果是为了获取一个对象(你的模型),并创build一个HTTP响应,其内容是来自模型对象的JSON格式的数据。

但是,传递给控制器​​的Json方法的是JSON格式的string对象 ,所以它将string对象 “序列化”为JSON,这就是为什么HTTP响应的内容被双引号(I'假设这是问题)。

我认为你可以考虑使用Content操作结果作为Json操作结果的替代方法,因为你基本上已经有了可用HTTP响应的原始内容。

 return this.Content(returntext, "application/json"); // not sure off-hand if you should also specify "charset=utf-8" here, // or if that is done automatically 

另一种方法是将服务中的JSON结果反序列化为一个对象,然后将该对象传递给控制器​​的Json方法,但缺点是您需要反序列化,然后重新序列化数据,这可能是不必要的为了你的目的。

您只需要返回标准的ContentResult并将ContentType设置为“application / json”。 你可以为它创build自定义的ActionResult:

 public class JsonStringResult : ContentResult { public JsonStringResult(string json) { Content = json; ContentType = "application/json"; } } 

然后返回它的实例:

 [HttpPost] public JsonResult UpdateBatchSearchMembers() { string returntext; if (!System.IO.File.Exists(path)) returntext = Properties.Settings.Default.EmptyBatchSearchUpdate; else returntext = Properties.Settings.Default.ResponsePath; return new JsonStringResult(returntext); } 

是的,这是没有进一步的问题,避免原始stringJSON就是这样。

  public ActionResult GetJson() { var json = System.IO.File.ReadAllText( Server.MapPath(@"~/App_Data/content.json")); return new ContentResult { Content = json, ContentType = "application/json", ContentEncoding = Encoding.UTF8 }; } 

注意:请注意, JsonResult方法返回typesJsonResult用于我,因为JsonResultContentResult都inheritance了ActionResult但它们之间没有任何关系。