【问题标题】:ServiceStack AutoQuery partial response returns unwanted GUID, int etcServiceStack AutoQuery 部分响应返回不需要的 GUID、int 等
【发布时间】:2015-08-15 00:36:07
【问题描述】:

我正在处理ServiceStack对AutoQuery的部分响应,代码sn-ps如下:

public class SalesOrderServices : MyService
    {
        Utilities.RequestUtilities utilities = new Utilities.RequestUtilities();

        public object Any(SalesOrdersGet request)
        {
            String qString = GetSelectString(base.Request);

            var q = AutoQuery.CreateQuery(request, Request.GetRequestParams());

            if (!qString.IsNullOrEmpty())
            {
                q.Select(qString);
            }

            return AutoQuery.Execute(request, q);
        }

继承MyService

namespace WombatWS.ServiceInterface.Sales
{
    public class MyService : Service
    {
        public IAutoQuery AutoQuery { get; set; }

        public String GetSelectString(IRequest request)
        {
            String qString = "";

            if (!request.QueryString["fields"].IsNullOrEmpty())
            {
                String sFields = request.QueryString["fields"].ToString();
                String[] properties = sFields.Split(',');

                foreach (String s in properties)
                {
                    if (!s.Equals(properties.Last().ToString()))
                    {
                        qString += s + ",";
                    }
                    else
                    {
                        qString += s;
                    }
                }
            }
            return qString;
        }
    }
}

我注意到,除了我放入 ?field={field1},{field2}... 的有趣字段之外,不需要的 DateTime 将按原样返回,以及所有 int、GUID 类型也将返回为 0、0000-000000000-0000-00000 之类的东西.如何摆脱它们?谢谢。

【问题讨论】:

    标签: c# servicestack


    【解决方案1】:

    Guid(如整数)是一个值类型,每个值类型都必须有一个值,Guid 的默认值类型是new Guid(),即00000000-0000-0000-0000-000000000000

    如果您不希望值类型发出值,则需要使用可为空的Guid?。或者,您可以配置 ServiceStack 文本序列化程序,使其不发出具有默认值的值类型:

    JsConifg.ExcludeDefaultValues = true;
    

    另一种选择是通过implementing ShouldSerialize to ignore fields 使用默认值自定义序列化,例如:

    class Poco
    {
         public Guid Guid { get; set; }
    
        public bool? ShouldSerialize(string fieldName)
        {
            return fieldName == "Guid" ? Guid != default(Guid) : true;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-04
      • 2013-02-08
      • 2013-09-09
      相关资源
      最近更新 更多