0
有沒有實現這個代碼的一種更好的方式:序列化表單數據到web API
public Payment SendPaymentToSagePay(Payment paymentModel)
{
var webClient = new WebClient()
{
BaseAddress = "http://localhost:64317/api/PaymentStart"
};
string price = paymentModel.price.ToString();
string productId = paymentModel.productId.ToString();
string paymentMode = paymentModel.paymentMode.ToString();
string context = paymentModel.context.ToString();
var collection = new NameValueCollection();
collection.Add("title", paymentModel.title);
collection.Add("firstName", paymentModel.firstName);
collection.Add("lastName", paymentModel.lastName);
collection.Add("email", paymentModel.email);
collection.Add("context", context);
collection.Add("programmeName", paymentModel.programmeName);
collection.Add("productId", productId);
collection.Add("price", price);
collection.Add("cardHolderTitle", paymentModel.cardHolderTitle);
collection.Add("cardHolderLastName", paymentModel.cardHolderLastName);
collection.Add("cardHolderFirstName,", paymentModel.cardHolderFirstName);
collection.Add("cardHolderEmail", paymentModel.cardHolderEmail);
collection.Add("selfAddress1", paymentModel.selfAddress1);
collection.Add("selfAddress2", paymentModel.selfAddress2);
collection.Add("selfCity", paymentModel.selfCity);
collection.Add("selfCountry", paymentModel.selfCountry);
collection.Add("selfState", paymentModel.selfState);
collection.Add("selfPhone", paymentModel.selfPhone);
collection.Add("selfPostCode", paymentModel.selfPostCode);
collection.Add("otherAddress1", paymentModel.otherAddress1);
collection.Add("otherAddress2", paymentModel.otherAddress2);
collection.Add("otherCity", paymentModel.otherCity);
collection.Add("otherCountry", paymentModel.otherCountry);
collection.Add("otherState", paymentModel.otherState);
collection.Add("otherPhone", paymentModel.otherPhone);
collection.Add("otherPostCode", paymentModel.otherPostCode);
collection.Add("ipAddress", paymentModel.ipAddress);
collection.Add("additionalInfo", paymentModel.additionalInfo);
collection.Add("paymentMode", paymentMode);
byte[] responseBytes = webClient.UploadValues("", "POST", collection);
string response = Encoding.UTF8.GetString(responseBytes);
string decodedResponse = HttpUtility.UrlDecode(response);
JavaScriptSerializer js = new JavaScriptSerializer();
var payment = js.Deserialize<Payment>(decodedResponse);
return payment;
}
我使用這種方法來序列化我的支付模式,然後將其發送到外部網頁API,然後將這個數據到各種存儲庫。
有沒有更簡潔的方法來做到這一點?
理想情況下,我想要一個更多的MVC4和依賴注入(我目前使用Unity容器)友好的解決方案,我創建了這個序列化程序作爲臨時解決方案,同時我研究了其他方法,到目前爲止這是我唯一一次能夠成功實施。
任何意見將非常感激。
感謝這個答案可以使用此暫且但理想的我在看搬走從這個實現 – CryoFusion87 2013-05-01 13:47:14