<%@ 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