繁体   English   中英

WEB Api没有将枚举发送给客户端

[英]WEB Api not sending Enum to client

我从“ BaseSync”类继承了一个类“ ApplicationSetting”,该类又从“ BaseEntity”类继承了。 以下是我的课程结构。

 [DataContract(Namespace = HelpersServiceModel.DefaultUriNamespace)]
 public class ApplicationSetting : BaseSyncEntity 
 {
    [DataMember]
    public string SettingKey { get; set; }
 }



 [DataContract(Namespace = HelpersServiceModel.DefaultUriNamespace)]
 public abstract class BaseSyncEntity : BaseEntity
 {

 }
 [DataContract(Namespace = HelpersServiceModel.DefaultUriNamespace)]
 public abstract class BaseEntity : ICloneable, IComparable, IEntityState,        INotifyPropertyChanged, IDataErrorInfo
 {
    [DataMember(EmitDefaultValue = false)] 
    public Nullable<int> Id
    {
        get
        {
            return _id;
        }
        set
        {
            SetProperty(ref _id, value);  
        }
    }
    [DataMember(EmitDefaultValue = false)]
    public EntityState EntityState
    {
        get { return _entityState; }
        set { _entityState = value; }
    }

 }

现在的问题是,每当我在浏览器中检查我的类json对象时。 我没有得到EntitySate的Enum属性? 有什么原因吗? 我正在获取所有其他属性,例如SettingKey和Id,但我根本没有获取Enum。

我的枚举类结构如下

public enum EntityState
    {
        /// <summary>
        /// The Business Entity is unchanged and no action needs to be taken on it.
        /// </summary>
        Unchanged,

        /// <summary>
        /// The Business Entity has been created and has not been inserted into the database.
        /// </summary>
        Added,

        /// <summary>
        /// The Business Entity has been changed and requires updating in the database.
        /// </summary>
        Changed,

        /// <summary>
        /// The Business Entity has been marked for removal from the database.
        /// </summary>
        Removed
    }

您需要将ENUM定义为数据合同。

像下面的事情。

[DataContract(Name = "CarCondition")]
public enum CarConditionWithNumbers
{
    [EnumMember]
    New = 10,

    [EnumMember]
    Used = 20,

    [EnumMember]
    Rental = 30,
}

请参阅以下链接。

http://msdn.microsoft.com/en-us/library/aa347875.aspx

也有其他几个例子。

http://beyondrelational.com/modules/2/blogs/48/posts/10065/enumeration-in-datacontract-of-wcf.aspx

http://dotnetmentors.com/wcf-datacontract-with-enum-example.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM