Best way to create and post a form in codebehind in C#

前提是你 提交于 2020-01-14 03:31:38

问题


I'm using webforms and I need to create and POST an http form to a payment gateway for one of my clients. I'm using a user control that is displayed in a masterpage, however they already have an existing <form> on the page, so I'm pretty sure I cannot add another.

What is the best practice method to create and post a form in C# from the code behind? Currently I have the following:

var nvc = new System.Collections.Specialized.NameValueCollection
    {
        {"EWAY_ACCESSCODE", ewayResponse.AccessCode},
        {"EWAY_CARDNAME", txtCcName.Text},
        {"EWAY_CARDNUMBER", txtCcNumber.Text},
        {"EWAY_CARDEXPIRYMONTH", ddlCcExpMonth.SelectedValue},
        {"EWAY_CARDEXPIRYYEAR", ddlCcExpYear.SelectedValue},
        {"EWAY_CARDCVN", txtCcv.Text}
    };

    var client = new WebClient();
    client.UploadValues(ewayResponse.FormActionURL, nvc);

This does work, but is there a better / alternative way to do it?


回答1:


Aside from the previous suggestion:

  • why wouldn't a (simpler) Button.PostbackUrl work?

    <asp:Button ID="externalPost" runat="server" PostBackUrl="http://www.somewhere.com/" Text="Post Somewhere" />
    

Also, assuming that's a payment gateway you're referring to, you're doing a server-side POST, which has implications on PCI Compliance (vs. a direct form post from client/browser).

Hth.




回答2:


Here is the solution I use:

http://jerschneid.blogspot.com/2007/03/hide-form-tag-but-leave-content.html

Caveat (own blog post):

http://codersbarn.com/post/2008/03/08/Solution-to-ASPNET-Form-PayPal-Problem.aspx



来源:https://stackoverflow.com/questions/19897404/best-way-to-create-and-post-a-form-in-codebehind-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!