如何通过表单操作 - Web服务(WCF)中的“POST”传递数据?

时间:2012-01-06 09:18:24

标签: c# asp.net html wcf

我必须将输入字段的值发布到我的webservice。在网上搜索根本没有运气。我不允许使用AJAX / JSon来包装和发布数据,只能通过WCF。我坚持这一点。希望有人能提供帮助。

HTML文件:
我删除了一些字段以最小化空间消耗。

<%@ Page Language="C#" AutoEventWireup="ue" CodeBehind="WebForm1.aspx.cs" Inherits="BasePayment.Client.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//D HTML 4.01 ansitional//EN" "http://www.w3.org//html4/loose.d">
<html>
<body>
<form method="POST" name="frmPayment" action="">

Merchant ID
<input type="text" name="merchantId" value="1546" />

Payment Method
<input type="text" name="pMethod" value="VISA" />

Credit card expiry month - 2 digits.
<input type="text" name="epMonth" value="02" />

Credit card expiry year - 4 digits.
<input type="text" name="epYear" value="2012" />

Credit card number.
<input type="text" name="cardNo" value="4918914107195005" />

Credit Card Verification Code
<input type="text" name="securityCode" value="123" />

Credit card holder name
<input type="text" name="cardHolder" value="Juan Dela Cruz" />

Redirect to a URL upon failed ansaction:
<input type="text" name="failUrl" value="http://www.yahoo.com" />

Remark:
<input type="text" name="remark" value="Other Remarks" />

<input type="submit" value="Submit" id="btnSubmit"/>

</form>
</body>
</html>

WCF:

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using BasePayment.Entities;
using System.ServiceModel.Activation;

namespace BasePayment.WebService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class PaymentComponent : IPaymentComponent
    {
        public void SendPayment(PaymentInformation payInfo)
        {
            PaymentInformation newPayInfo = new PaymentInformation();

            /*Validation of input goes here ...
             * 
             * 
             *
             * */

            // Then send data.
        }
    }
}  

WCF的接口:

using System;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;

using BasePayment.Entities;

namespace BasePayment.WebService
{
    [ServiceContract(Namespace="http://test.com/services/payment")]
    public interface IPaymentComponent
    {
        [OperationContract]
        [WebInvoke(UriTemplate="PaymentInfo", Method="POST")]
        void SendPayment(PaymentInformation payInfo);
    }
}  

支付信息的实体:
我删除了一些属性以减少空间消耗。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace BasePayment.Entities
{
    public class PaymentInformation : IPaymentInformation
    {
        public PaymentInformation()
        {
        }

        public int ExpiryMonth
        {
            get;
            set;
        }

        public int ExpiryYear
        {
            get;
            set;
        }

        public int CreditCardVerificationCode
        {
            get;
            set;
        }

        public double Amount
        {
            get;
            set;
        }

        public string Language
        {
            get;
            set;
        }

        public string CardHolderName
        {
            get;
            set;
        }

        public string Remark
        {
            get;
            set;
        }

    }
}  

实体的接口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BasePayment.Entities
{
    interface IPaymentInformation
    {
        long CreditCardNumber { get; set; }
        long OrderReferenceID { get; set; }

        int CurrencyCode { get; set; }
        int MerchantID { get; set; }
        int ExpiryMonth { get; set; }
        int ExpiryYear { get; set; }
        int CreditCardVerificationCode { get; set; }

        double Amount { get; set; }

        string Language { get; set; }
        string CardHolderName { get; set; }
        string URLUponFailed { get; set; }
        string URLUponSuccess { get; set; }
        string URLUponError { get; set; }
        string Remark { get; set; }

        PaymentMethod PaymentMethod { get; set; }
    }
}  

这里是我项目的预览:

enter image description here

提前致谢!!!

1 个答案:

答案 0 :(得分:1)

你可以做的是将输入发送回服务器端代码,在这种情况下是WebForm1.aspx.cs,你需要做的是将输入设置为runat =“server”或使用aspx控件如以下内容:

    <asp:TextBox runat="server" id="merchantId" value="1546" />

您需要为所有输入执行此操作。 然后在Page load方法中你可以去:

    protected void Page_Load(object sender, EventArgs e)
    {
        if(this.IsPostBack)
        {
            PaymentInformation newPayInfo = new PaymentInformation();
            newPayInfo.MerchantID = merchantId.Text; // to get text
            /* Validation of input goes here ... */
            // Then send data.
        }
    }

这意味着您不需要使用WCF就可以使用该页面。

希望这是有道理的