WebService-example

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebService.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <asp:TextBox ID="Num1" runat="server"></asp:TextBox>   
             <select id="selectOper" runat = "server">   
                 <option>+</option>   
                 <option>-</option>   
                 <option>*</option>   
                 <option>/</option>   
             </select>   
             <asp:TextBox ID="Num2" runat="server"></asp:TextBox>   
        <asp:Button ID="Button1" runat="server" Text="=" οnclick="Button1_Click" />   
             <asp:TextBox ID="Result" runat="server"></asp:TextBox>   
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebService.ServiceReference1;

namespace WebService
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string selectFlag = selectOper.Value;
            //localhost.Service web = new localhost.Service();
            ServiceReference1.WebServiceSoapClient client = new WebServiceSoapClient();
            //client.addition()
            if (selectFlag.Equals("+"))
            {
                Result.Text = (client.addition(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
            }
            else if (selectFlag.Equals("-"))
            {
                Result.Text = (client.subtract(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
            }
            else if (selectFlag.Equals("*"))
            {
                Result.Text = (client.multiplication(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
            }
            else if (selectFlag.Equals("/"))
            {
                Result.Text = (client.division(double.Parse(Num1.Text), double.Parse(Num2.Text))).ToString();
            }
        }   
    }
}

 

Web Config

 

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <defaultDocument>
      <files>
        <add value="WebService.asmx"/>
      </files>
    </defaultDocument>
  </system.webServer>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="WebServiceSoap" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:63642/WebService.asmx" binding="basicHttpBinding"
        bindingConfiguration="WebServiceSoap" contract="ServiceReference1.WebServiceSoap"
        name="WebServiceSoap" />
    </client>
  </system.serviceModel>
</configuration>

 

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

namespace WebService
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {
        //[WebMethod]
        //public string HelloWorld()
        //{
        //    return "Hello World";
        //}
        public WebService()
        {
            //InitializeComponent();    
        }
        //[WebMethod]   
        //public string HelloWorld()   
        //{   
        //    return "Hello World";   
        //}   
        //[WebMethod]
        //public string HelloWorld()
        //{
        //    return "Hello World";
        //}
        [WebMethod(Description = "Add")]
        public double addition(double i, double j)
        {
            return i + j;
        }
        [WebMethod(Description = "Dis")]
        public double subtract(double i, double j)
        {
            return i - j;
        }
        [WebMethod(Description = "C")]
        public double multiplication(double i, double j)
        {
            return i * j;
        }
        [WebMethod(Description = "CH")]
        public double division(double i, double j)
        {
            if (j != 0)
                return i / j;
            else
                return 0;
        }
    }
}
 

 

部分转自:

https://www.cnblogs.com/mahaisong/archive/2011/10/11/2207537.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值