【问题标题】:serializing form data to a web api将表单数据序列化为 Web api
【发布时间】:2013-05-01 12:30:30
【问题描述】:

有没有更好的方法来实现这个代码:

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;
    }

我正在使用这种方法来序列化我的支付模型,然后将其发送到外部 Web api,然后将这些数据添加到各种存储库中。

有没有更简洁的方法来做到这一点?

理想情况下,我想要一个对 MVC4 和依赖注入(我目前正在使用 Unity 容器)更友好的解决方案,我创建了这个序列化程序作为临时解决方案,同时我研究了其他方法,到目前为止,这是我唯一能够做到的成功实施。

非常感谢任何建议。

【问题讨论】:

    标签: c# json serialization asp.net-web-api


    【解决方案1】:

    您可以发布 JSON 而不是表单集合吗?如果可以的话,您可以使用与您相同的 JavaScriptSerializer 来反序列化对 serialize 的响应@您的 Payment

    string serializedPayment = js.Serialize(paymentModel);

    【讨论】:

    • 感谢这个答案可能暂时使用这个,但理想情况下我正在考虑远离这个实现
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2012-07-05
    • 1970-01-01
    相关资源
    最近更新 更多