public class JsonResult : ActionResult
{
public JsonResult()
{
this.JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.DenyGet;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if ((this.JsonRequestBehavior == System.Web.Mvc.JsonRequestBehavior.DenyGet) && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
}
HttpResponseBase response = context.HttpContext.Response;
if (!string.IsNullOrEmpty(this.ContentType))
{
response.ContentType = this.ContentType;
}
else
{
response.ContentType = "application/json";
}
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Data != null)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
response.Write(serializer.Serialize(this.Data));
}
}
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public System.Web.Mvc.JsonRequestBehavior JsonRequestBehavior { get; set; }
}
}
/// <summary>
/// 自定义Json视图
/// </summary>
public class CustomJsonResult:JsonResult
{
/// <summary>
/// 格式化字符串
/// </summary>
public string FormateStr
{
get;
set;
}
/// <summary>
/// 重写执行视图
/// </summary>
/// <param name="context">上下文</param>
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
if (string.IsNullOrEmpty(this.ContentType))
{
response.ContentType = this.ContentType;
}
else
{
response.ContentType = "application/json";
}
if (this.ContentEncoding != null)
{
response.ContentEncoding = this.ContentEncoding;
}
if (this.Data != null)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
string jsonString = jss.Serialize(Data);
string p = @"\\/Date\((\d+)\)\\/";
MatchEvaluator matchEvaluator = new MatchEvaluator(this.ConvertJsonDateToDateString);
Regex reg = new Regex(p);
jsonString = reg.Replace(jsonString, matchEvaluator);
response.Write(jsonString);
}
}
/// <summary>
/// 将Json序列化的时间由/Date(1294499956278)转为字符串 .
/// </summary>
/// <param name="m">正则匹配</param>
/// <returns>格式化后的字符串</returns>
private string ConvertJsonDateToDateString(Match m)
{
string result = string.Empty;
DateTime dt = new DateTime(1970, 1, 1);
dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
dt = dt.ToLocalTime();
result = dt.ToString(FormateStr);
return result;
}
}
protected internal JsonResult Json(object data)
{
return this.Json(data, null, null, JsonRequestBehavior.DenyGet);
}
protected internal JsonResult Json(object data, string contentType)
{
return this.Json(data, contentType, null, JsonRequestBehavior.DenyGet);
}
protected internal JsonResult Json(object data, JsonRequestBehavior behavior)
{
return this.Json(data, null, null, behavior);
}
protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding)
{
return this.Json(data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
}
protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior)
{
return this.Json(data, contentType, null, behavior);
}
protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior };
}
/// <summary>
/// 返回JsonResult
/// </summary>
/// <param name="data">数据</param>
/// <param name="contentType">内容类型</param>
/// <param name="contentEncoding">内容编码</param>
/// <param name="behavior">行为</param>
/// <returns>JsonReuslt</returns>
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return new CustomJsonResult
{
Data = data,
ContentType = contentType,
ContentEncoding =contentEncoding,
JsonRequestBehavior = behavior,
FormateStr = "yyyy-MM-dd HH:mm:ss"
};
}
/// <summary>
/// 返回JsonResult.24 /// </summary>
/// <param name="data">数据</param>
/// <param name="behavior">行为</param>
/// <param name="format">json中dateTime类型的格式</param>
/// <returns>Json</returns>
protected JsonResult MyJson(object data, JsonRequestBehavior behavior,string format)
{
return new CustomJsonResult
{
Data = data,
JsonRequestBehavior = behavior,
FormateStr = format
};
}
/// <summary>
/// 返回JsonResult42 /// </summary>
/// <param name="data">数据</param>
/// <param name="format">数据格式</param>
/// <returns>Json</returns>
protected JsonResult MyJson(object data, string format)
{
return new CustomJsonResult
{
Data = data,
FormateStr = format
};
}
public class ProjectMileStoneController : BaseController
{
/// <summary>
/// 首页视图
/// </summary>
/// <returns>视图</returns>
public ActionResult Index()
{
return this.View();
}
#region 项目里程碑查询
/// <summary>
/// 根据项目编号获取项目里程碑
/// </summary>
/// <param name="projectId">项目编号</param>
/// <returns>项目里程碑</returns>
public JsonResult GetProjectMileStoneByProjectId(int projectId)
{
IList<ProjectMileStone> projectMileStones = FacadeContainer.Get<IProjectMileStoneService>().GetProjectMileStonesByProjectId(projectId);
return this.MyJson(projectMileStones, "yyyy.MM.dd");
}
#endregion
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有