/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System.Web.Mvc;
using Everest.Library.Extjs;
using Everest.Library.ExtensionMethod;
using System.Text;
namespace Everest.Library.Mvc{
public abstract class ExtControllerBase : EverestControllerBase
{
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding)
{
JsonResult jsonResult = new CustomJsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding };
return WrapperJsonResult(jsonResult);
}
private JsonResult WrapperJsonResult(JsonResult jsonResult)
{
//ref:http://extjs.com/forum/showthread.php?t=8129
//You can (in fact you must) return JSON from a file upload submit, but your content type must be text/html.
if (Request.ContentType.ToLower().Contains("multipart/form-data") || Request.Files.Count > 0)
{
jsonResult.ContentType = "text/html";
}
return jsonResult;
}
/// <summary>
/// Creates a <see cref="T:System.Web.Mvc.JsonResult"/> object that serializes the specified object to JavaScript Object Notation (JSON) format using the content type, content encoding, and the JSON request behavior.
/// </summary>
/// <param name="data">The JavaScript object graph to serialize.</param>
/// <param name="contentType">The content type (MIME type).</param>
/// <param name="contentEncoding">The content encoding.</param>
/// <param name="behavior">The JSON request behavior</param>
/// <returns>
/// The result object that serializes the specified object to JSON format.
/// </returns>
protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
var jsonResult = new CustomJsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
return WrapperJsonResult(jsonResult);
}
protected override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
filterContext.Result = Json(new JsonResultData() { success = false, message = filterContext.Exception.Message });
filterContext.ExceptionHandled = true;
}
}
}
|