ASP.NET WEB API回发到客户端消息体的格式化

首先基于SOA的消息通信基本都是按照一个统一的协议规范进行交互,那么必须定义消息体。不同的交互结构设计了不同的消息体。

业界统一使用的SOAP是一种规范的XML消息内容。例如在WCF中返回的结果。

随着移动互联网的盛行,Rest交互的方式越来越受青睐。

微软的MVC3之后的版本,合并了Rest WCF,越来越强大。也就是现在ASP.NET web API.

基本用法就不提了,这里主要解说的是自定义消息体的实现:

1:消息体结构:

[DataContract]
    public class SendToClientMsgConteiner
    {
        /// <summary>
        /// 结果状态
        /// </summary>
        [DataMember(Name = "_s")]
        public MessageStatus Status { get; set; }
 
        private string _Message = "";
        /// <summary>
        /// 消息
        /// </summary>
        [DataMember(Name = "_m")]
        public string Message
        {
            get { return _Message; }
            set { _Message = value; }
        }
 
        /// <summary>
        /// 当前请求结果集
        /// </summary>
        [DataMember(Name = "_d")]
        public IEnumerable<IBusinessObject> Results { get; set; }
 
    }
    public enum MessageStatus
    {
        /// <summary>
        /// 失败
        /// </summary>
        Error = 0,
        /// <summary>
        /// 成功
        /// </summary>
        Success = 1
    }

注意,这里涉及到.net 底层的对象序列化,如果需要自定义字段内容,可以使用此属性 DataContract ,屏蔽的话 可以使用JsonIgnore 。
2 控制器中的Action 在执行完毕后 返回此对象,即可自动将对象进行序列化为Json数据

 [HttpPost]
        [MessageFormator]
        public SendToClientMsgConteiner CalcReceiveClientProductPrice(ViewModelForCalcProductPriceClient clientData)
        {
            SendToClientMsgConteiner msg = new SendToClientMsgConteiner();
 
            try
            {
                var priceItems = bll_ProductService.CalcReceiveClientProductPrice(clientData);
                msg.Status = MessageStatus.Success;
                msg.Results = new List<ViewModelForCalcProductPriceServer> { priceItems };
            }
            catch (Exception ex)
            {
                msg.Status = MessageStatus.Error;
                msg.Message = ex.ToString();
            }
 
            return msg;
        }

3 如果需要对返回的内容进行过滤或者其它操作;那么可以为这个Action添加Filter
    /// <summary>
    /// 消息执行/返回的时候 拦截器
    /// </summary>
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
    public class MessageFormatorAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext filterContext)
        {
            base.OnActionExecuting(filterContext);
        }
 
        public override void OnActionExecuted(HttpActionExecutedContext filterContext)
        {
            
            var result = filterContext.Response.Content as ObjectContent<SendToClientMsgConteiner>;
 
            if (null==result)
            {
                //不是自定义的SOAP消息 那么返回系统处理消息内容
                base.OnActionExecuted(filterContext);
                return;
            }
 
            var msg = result.Value as SendToClientMsgConteiner;
            if (msg.Status== MessageStatus.Success)
            {
                //成功返回 没有错误消息 那么不需要消息体属性
                var content = new SendToClientMsgConteinerWithNoError
                {
                    Status = MessageStatus.Success,
                    Results = msg.Results
                };
 
                filterContext.Response.Content = new ObjectContent<SendToClientMsgConteinerWithNoError>(content, result.Formatter);
            }
 
            //最后调用SDK 进行消息内容处理回发
            base.OnActionExecuted(filterContext); 
           
 
        }
    }


以上方式就可以随意的进行自定义的消息内容操作订制了。
原文地址:https://www.cnblogs.com/micro-chen/p/4191820.html