美好的一天
我有一个要序列化并传递给Controller的列表,在单击按钮时,我有requestList
并填充了数据
List<LabOrderLineItemRequest> requestList = new List<LabOrderLineItemRequest>();
这里是string responseList
,我为此调用了方法来序列化数据
string responseList = Project.DTO.Helpers.MessageRelay.SendRequests(uriStringList, "POST", "application/json", requestList, _sessionToken);
List<LabOrderLineItemResponse> objList = ((List<LabOrderLineItemResponse>)(Project.DTO.Serialization.ObjectXmlSerialization.DeserializeFromXML(responseList.ToString(), typeof(List<LabOrderLineItemResponse>))));
if ((!(objList == null) && (objList.GetType() == typeof(List<LabOrderLineItemResponse>))))
{
}
这是进行序列化的方法
public static string SendRequest(string uriString, string Method, string ContentType, object postBody, string Base64Key)
{
System.Net.HttpWebRequest myWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(uriString);
myWebRequest.Method = Method;
myWebRequest.ContentType = ContentType;
myWebRequest.Timeout = 300000;
byte[] byteArray = Encoding.UTF8.GetBytes(string.Format(xml, Base64Key));
myWebRequest.ContentLength = byteArray.Length;
Stream dataStream = myWebRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
try
{
System.Net.WebResponse response = myWebRequest.GetResponse();
//Read response
System.IO.StreamReader objStreamReader = new System.IO.StreamReader(response.GetResponseStream());
string output = objStreamReader.ReadToEnd();
output = output.Replace("<string>", "").Replace("</string>", "");
string dcypher = "";
try
{
dcypher = Encryption.AESClass.AESDecrypt(output, Base64Key);
}
catch (Exception)
{
dcypher = output;
}
return dcypher;
}
catch (Exception e)
{
return e.Message;
}
}
最后,SendRequest
将调用控制器,但是问题是,当代码调用控制器时,它将在request
上传递NULL。 object postBody
确实带有即时传递的列表,因此唯一的问题是在对数据进行序列化之后,然后不将其传递给控制器。如果您不理解,请告诉我,以便我再次阐述。谢谢
[System.Web.Http.Route("api/lineitem")]
[System.Web.Http.HttpPost()]
public List<LabOrderLineItemResponse> LabOrderLineItem(List<LabOrderLineItemRequest> request)
{
AuthenticateITProcess lab = new AuthenticateITProcess();
// return lab.CreateLabLineItem(request);
return null;
}
答案 0 :(得分:0)
您期望发送到控制器的对象,无论是Json还是XML。但是您正在以不支持的格式加密字符串。因此,当控制器对消息进行反序列化时,由于您未使用格式而无法实现。
如果要使用自己的特定加密算法和对象格式,则需要将一个字符串放入控制器中,然后自己对它进行反序列化。
答案 1 :(得分:0)
所以我要做的是将Web.UI Webform中的项目列表直接传递给控制器。 他就是我做到的。我正在将值传递给控制器。
string uriStringList = (uriRoutePrefix);
string inputJson = (new JavaScriptSerializer()).Serialize(requestList);
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(uriStringList));
httpRequest.Accept = "application/json";
httpRequest.ContentType = "application/json";
httpRequest.Method = "POST";
byte[] bytes = Encoding.UTF8.GetBytes(inputJson);
using (Stream stream = httpRequest.GetRequestStream())
{
stream.Write(bytes, 0, bytes.Length);
stream.Close();
}
using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
{
using (Stream stream = httpResponse.GetResponseStream())
{
string lblOutput = (new StreamReader(stream)).ReadToEnd();
}
}