MVC返回JSON,IE下无法接收JSON,IE下JSON提示另存为

    public class JsonAttribute : ActionFilterAttribute 
    {
        /// <summary>
        /// 兼容IE下无法接受JSON问题
        /// </summary>
        /// <param name="filterContext"></param>
        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            string contentType = filterContext.HttpContext.Response.ContentType;
            if (contentType == "application/json")
                filterContext.HttpContext.Response.ContentType = "text/html";


            base.OnResultExecuted(filterContext);
        }
    }

写一个过滤器,ContentType修改为"text/html",类名要以Attribute结尾

在Global.asax文件中注册过滤器

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new JsonAttribute());
            filters.Add(new HandleErrorAttribute());
        }

 

 

posted @ 2013-05-22 13:22  三线程序员  阅读(2331)  评论(3编辑  收藏  举报