问问题
1284 次
1 回答
2
为了解决这个问题,我从 JsonResult 继承并在那里添加了 textarea
using System;
using System.Web.Mvc;
using System.Web.Script.Serialization;
namespace TinyHouseMap.Web.Infrastructure.Results
{
public class JsonInIframeResult : JsonResult
{
public bool EncloseInTextArea
{
get;
set;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if ((JsonRequestBehavior == JsonRequestBehavior.DenyGet)
&& string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("JsonRequest GetNotAllowed");
}
var response = context.HttpContext.Response;
response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json";
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
var serializer = new JavaScriptSerializer();
string results;
if (EncloseInTextArea)
{
results = "<textarea>" + serializer.Serialize(Data) + "</textarea>";
}
else
{
results = serializer.Serialize(Data);
}
response.Write(results);
}
}
}
}
然后在我的控制器基类中创建了一个助手
protected JsonInIframeResult JsonInIframe(object data, string contentType, bool encloseInTextArea)
{
var result = new JsonInIframeResult {Data = data, ContentType = contentType, EncloseInTextArea = encloseInTextArea};
return result;
}
于 2012-03-28T00:47:18.653 回答