问题描述
无需序列化从 API 响应中获取数据
var apiResponseDetails = authorityApiResponse.Content.ReadAsstringAsync().Result;
"[
{
\"<RoleName>k__backingField\":\"L5 _Admin _Role\",\"<RoleType>k__backingField\":\"565,1\"
}
]"
虽然反序列化 RoleName
和 RoleType
的相同响应为 null
lstRole = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ChangeRoleNotification>>(Convert.ToString(apiResponseDetails));
Name Value
lstRole Count = 1
[0] {Presentation.Common.ChangeRoleNotification}
RoleName null
RoleType null
[Serializable]
public class ChangeRoleNotification
{
[Newtonsoft.Json.JsonProperty(PropertyName = "RoleName")]
public string RoleName { get; set; }
[Newtonsoft.Json.JsonProperty(PropertyName = "RoleType")]
public string RoleType { get; set; }
}
...
GetUserRolesDetailsRequest getUserRolesDetails = new GetUserRolesDetailsRequest();
getUserRolesDetails.UserID = objUser.UserId;
getUserRolesDetails.RoleName = HttpContext.Current.Session["UserTypeName"].ToString();
getUserRolesDetails.RoleType = HttpContext.Current.Session["RoleType"].ToString();
getUserRolesDetails.RoleID = Convert.ToInt64(HttpContext.Current.Session["RoleID"]);
System.Net.Http.HttpResponseMessage authorityApiResponse = new System.Net.Http.HttpResponseMessage(false ? HttpStatusCode.OK : HttpStatusCode.BadRequest);
System.Net.Http.HttpContent RequestDetails = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(getUserRolesDetails),Encoding.UTF8);
if (!String.IsNullOrEmpty(GetWebConfigkeyvalueStatic("FrameworkApiURL")))
{
authorityApiResponse = API.PostAPIAsJson($"{GetWebConfigkeyvalueStatic("FrameworkApiURL")}Category/GetCategoryUserRolesDetails",RequestDetails);
if (authorityApiResponse != null && authorityApiResponse.StatusCode == HttpStatusCode.OK && authorityApiResponse.Content.ReadAsstringAsync().Result != null)
{
var apiResponseDetails = authorityApiResponse.Content.ReadAsstringAsync().Result;
lstRole = new List<ChangeRoleNotification>();
lstRole = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ChangeRoleNotification>>(Convert.ToString(apiResponseDetails));
}
}
由于我正在将项目转换为 ASP.NET MVC 3-Tier,我看到一个奇怪的 k__backingField
是什么意思?
更新了响应 API 的服务器端片段:
[Route("Category/GetCategoryUserRolesDetails")]
[ActionName("GetCategoryUserRolesDetails")]
[HttpPost]
public HttpResponseMessage GetCategoryUserRolesDetails(CategoryRequestDetails categoryRequestDetails)
{
List<ChangeRoleNotification> response = null;
FrameworkAPIs.Log.LogClass.log.Debug("\nModule Name : Category;\nMethod Name : GetCategoryUserRolesDetails;\n Message :GetCategoryUserRolesDetails method starts ");
string statusCode = String.Empty;
DateTime StartTime = DateTime.Now;
DateTime EndTime = DateTime.Now;
Int64 WebServiceLogID = (new ServiceLogGenerator()).GenerateLog(categoryRequestDetails,"Category","GetCategoryUserRolesDetails",StartTime,EndTime,"",null,0);
try
{
ManageCategory mangageCategory = new ManageCategory();
if (categoryRequestDetails.UserID > 0)
response = mangageCategory.GetCategoryUserRolesDetails(categoryRequestDetails);
else
return Request.CreateErrorResponse(HttpStatusCode.BadRequest,"Invalid UserID");
}
catch (Exception ex)
{
FrameworkAPIs.Log.LogClass.log.Error("\nModule Name : Category;\nMethod Name : GetCategoryUserRolesDetails;\nError Message : " + ex.Message + ex.StackTrace + "\n");
(new ServiceLogGenerator()).GenerateLog(null,DateTime.Now,ex.StackTrace + ";\n" + ex.Message,WebServiceLogID);
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError,ex);
}
(new ServiceLogGenerator()).GenerateLog(null,response,WebServiceLogID);
FrameworkAPIs.Log.LogClass.log.Debug("\nModule Name : Category;\nMethod Name : GetCategoryUserRolesDetails;\n Message :GetCategoryUserRolesDetails method ends\nResult :" + response);
return Request.CreateResponse<List<ChangeRoleNotification>>(HttpStatusCode.OK,response);
}
解决方法
我认为这不是一个合适的解决方案,因为添加了 [JsonObject]
属性而不是 [Serializable]
。 k__BackingField
和 null
的问题已解决。
如果有人确切地知道如何通过添加 [Serializable]
来处理这个问题,欢迎他们
[Newtonsoft.Json.JsonObject]
public class ChangeRoleNotification
{
public string RoleName { get; set; }
public string RoleType { get; set; }
}
使用以下方法更新
方法一: k__BackingField
消失了。但是 null
似乎在服务器端持续存在
[Serializable]
public class ChangeRoleNotification
{
private string RoleNameField;
public string RoleName
{
get { return RoleNameField; }
set { RoleNameField = value; }
}
private string RoleTypeField;
public string RoleType
{
get { return RoleTypeField; }
set { RoleTypeField = value; }
}
}
方法 II(有效):k__BackingField
和 null
都消失了
[Serializable,DataContract]
public class ChangeRoleNotification
{
[DataMember]
public string RoleName { get; set; }
[DataMember]
public string RoleType { get; set; }
}
通过在类中添加 [DataContract]
并在方法 II 中解决的仅服务器端单独为属性添加 [DataMember]
,我认为这是最合适的