servicestack,C#,Asp.net Web Api,Serialization,Deserialization,servicestack" /> servicestack,C#,Asp.net Web Api,Serialization,Deserialization,servicestack" />

C# 默认格式化程序为servicestack时Web Api中的ModelState验证

C# 默认格式化程序为servicestack时Web Api中的ModelState验证,c#,asp.net-web-api,serialization,deserialization,servicestack,C#,Asp.net Web Api,Serialization,Deserialization,servicestack,我有一个WEB.API控制器,它使用属性进行modelstate验证,当我使用WEB API的默认序列化程序时,一切正常,但当我将其更改为bool值和EnumDataType(typeof(VehicleType))的servicestack必需属性时,就不起作用了。我的意思是,它表明模型状态是有效的,但事实并非如此,我是这样配置servicestack的: 在Owin启动中: private void ConfigureServicestack(HttpConfiguration config

我有一个WEB.API控制器,它使用属性进行modelstate验证,当我使用WEB API的默认序列化程序时,一切正常,但当我将其更改为bool值和EnumDataType(typeof(VehicleType))的servicestack必需属性时,就不起作用了。我的意思是,它表明模型状态是有效的,但事实并非如此,我是这样配置servicestack的: 在Owin启动中:

private void ConfigureServicestack(HttpConfiguration config)
{
    config.Formatters.RemoveAt(0);
    config.Formatters.Insert(0, new ServiceStackTesxtFormatter());
}
以及servicestack的此类:

public class ServiceStackTextFormatter : MediaTypeFormatter
{
    public ServiceStackTextFormatter()
    {
        JsConfig.ConvertObjectTypesIntoStringDictionary = true;
        SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

        SupportedEncodings.Add(new UTF8Encoding(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true));
        SupportedEncodings.Add(new UnicodeEncoding(bigEndian: false, byteOrderMark: true, throwOnInvalidBytes: true));
    }

    public override bool CanReadType(Type type)
    {
        if (type == null) throw new ArgumentNullException("type");
        return true;
    }

    public override bool CanWriteType(Type type)
    {
        if (type == null) throw new ArgumentNullException("type");
        return true;
    }

    public override Task<object> ReadFromStreamAsync(Type type, Stream readStream, System.Net.Http.HttpContent content, IFormatterLogger formatterLogger)
    {
        var task = Task<object>.Factory.StartNew(() => JsonSerializer.DeserializeFromStream(type, readStream));
        return task;
    }

    public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, TransportContext transportContext)
    {
        var task = Task.Factory.StartNew(() => JsonSerializer.SerializeToStream(value, type, writeStream));
        return task;
    }
}

奇怪的问题是,当我从应用程序启动中删除ConfigureServicestack时,一切正常,我想如果其中一个属性为null或客户端servicestack未提供,可能会将其淡化为默认值,例如,对于IsOwner,将其设置为:“IsOwner”:false,但是我不知道它是否正确,或者我如何才能关闭此功能

我找到了答案,我不知道是否有更好的方法,但是通过标记bool value nullable,它可以很好地用于bool值,这样:

public bool? IsOwner { get; set; }
public bool? IsOwner { get; set; }