MVC开发中自定义返回类型

在做项目web的MVC中,会用到返回值的问题,我们一般使用AjaxResult的返回值,根据自己的需要进行自定义,如下参考:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ToolsUse.CommonHelper
{
    public class AjaxResult
    {
        /// <summary>
        /// 是否产生错误
        /// </summary>
        public bool IsError { get; set; }

        /// <summary>
        /// 错误信息,或者成功信息
        /// </summary>
        public string Message { get; set; }

        /// <summary>
        /// 成功可能时返回的数据
        /// </summary>
        public object Data { get; set; }

        #region Error
        public static AjaxResult Error()
        {
            return new AjaxResult
            {
                IsError = true
            };
        }
        public static AjaxResult Error(string message)
        {
            return new AjaxResult
            {
                IsError = true,
                Message = message
            };
        }
        public static AjaxResult Error(object data, string message)
        {
            return new AjaxResult
            {
                IsError = true,
                Message = message,
                Data = data
            };
        }
        #endregion

        #region Success
        public static AjaxResult Success()
        {
            return new AjaxResult
            {
                IsError = false
            };
        }
        public static AjaxResult Success(string message)
        {
            return new AjaxResult
            {
                IsError = false,
                Message = message
            };
        }
        public static AjaxResult Success(object data)
        {
            return new AjaxResult
            {
                IsError = false,
                Data = data
            };
        }
        public static AjaxResult Success(object data, string message)
        {
            return new AjaxResult
            {
                IsError = false,
                Data = data,
                Message = message
            };
        }
        #endregion

        /// <summary>
        /// 返回当前对象JSON字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return JSON.FormatString(this);
        }
    }
}

另外,你可以修改返回错误的时候,对应的Exception对象。

 使用就简单了,如下调用:

    public class TestController : Controller
    {
        // GET: UpYunTest
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Test1()
        {
            return View();
        }

        public JsonResult UploadTest()
        {
            //文件检测
            if (Request.Files.Count == 0)
            {
                return Json(AjaxResult.Error("无要上传的文件"));
            }
            var file = Request.Files[0];
            if (file == null)
            {
                return Json(AjaxResult.Error("无要上传的文件"));
            }
            if (file.ContentLength > 2 * 1024 * 1024)
            {
                return Json(AjaxResult.Error("文件过大"));
            }
            var extensionName = Path.GetExtension(file.FileName);
            if (!CloudFileHelper.ImageExtensions.Contains(extensionName))
            {
                return Json(AjaxResult.Error("请上传图片格式的文件"));
            }
            var bucketType = UpyunHelper.GetFileType(extensionName+ "_comprehensive ");//设置只传到综合的服务器kwan-upyun中
            var fileName = UpyunHelper.BuildFileName(extensionName);
            var filePath = UpyunHelper.BuildFilePath(1);
            var url=UpyunHelper.UpLoad(file.InputStream,bucketType,filePath,fileName);
            var rtnUrl = UpyunHelper.ProcessUrl(url,bucketType);
            return Json(AjaxResult.Success(rtnUrl, "上传成功"));
        }
    }

 

posted on 2019-10-07 14:46  jack_Meng  阅读(661)  评论(0编辑  收藏  举报

导航