serializing form data to a web api

is there a better way of implementing this code:

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

I am using this method to serialize my payment model and then send it to an external web api which then adds this data to various repositories.

Is there a more succinct method of doing this?

Ideally I want a solution that's more MVC4 and Dependency Injection (I am currently using the Unity container) friendly, I created this serializer as an interim solution whilst I researched other methods, so far this is the only one I have been able to successfully implement.

Any advice would be very much appreciated.

Answers


Can you post JSON instead of a form collection? If you can, you can use the same JavaScriptSerializer you're using to deserialize the response to serialize your Payment:

string serializedPayment = js.Serialize(paymentModel);


Need Your Help

How to set timeout for each request using Apache httpclient 4.* with PoolingHttpClientConnectionManager?

java apache-httpclient-4.x connection-timeout

How to use the Apache httpclient 4.3 and PoolingHttpClientConnectionManager and pass the ConnectTimeOut and ReadTimeOut for each request.

TSQL try catch transaction error handling, transaction count mismatch

c# sql-server tsql ado.net transactions

I have a sproc that I am calling from C# with a transaction by doing:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.