猿问

asp.net core 的webapi返回的属性对名字是小名?

public class JsonResult<T>

{   
      public JsonResult()    {
       Value = default(T);
    }    
    public string Status { set; get; }    
    public T Value { set; get; } 
}
public async Task<JsonResult<string>> LoginApi(User userFromFore)
搞了我老半天发现原来返回的json是 status而不时Status.问题来了,我偏要保持大小写怎么办?


狐的传说
浏览 625回答 2
2回答

慕容708150

你主动把它序列化成字符串呗。using Newtonsoft.Json;namespace xxx{public class JsonResult<T>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;public JsonResult()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Value = default(T);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("Status")]&nbsp; &nbsp; &nbsp; &nbsp; public string Status { set; get; }&nbsp; &nbsp; &nbsp; &nbsp; [JsonProperty("Value")]&nbsp; &nbsp; &nbsp; &nbsp; public T Value { set; get; }&nbsp;&nbsp; &nbsp; }}

智慧大石

core里很多json序列化的地方都比改成驼峰明明了.包括signalr.解决办法是修改全局json序列化配置比如&nbsp; &nbsp; &nbsp; &nbsp; services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
随时随地看视频慕课网APP
我要回答