将表单数据序列化到web api

时间:2013-05-01 12:30:31

标签: c# json serialization asp.net-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容器)的解决方案,我创建了这个序列化器作为临时解决方案,同时我研究了其他方法,到目前为止这是我唯一能够成功实施。

非常感谢任何建议。

1 个答案:

答案 0 :(得分:0)

您可以发布JSON而不是表单集吗?如果可以,您可以使用相同的JavaScriptSerializer将您的Payment serialize的响应反序列化:

string serializedPayment = js.Serialize(paymentModel);