Create WCF REST service.
Create ASP.NET website to consume service.
Step 1(Creating Service)
IService1.cs
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- [WebInvoke(Method = "GET", UriTemplate = "Add/{num1}/{num2}")]
- string Add(string num1, string num2);
- }
- Service1.svc.cs
- public string Add(string num1, string num2)
- {
- int res = Convert.ToInt32(num1) + Convert.ToInt32(num2);
- return "Result is : " + res.ToString();
- }
Step 2(Creating website)
Default.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
- <!DOCTYPE html>
- <html
- xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- Enter First Number
-
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <br />
- Enter Second Number
- <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
- <br />
- <asp:Button ID="btn_Add" runat="server" OnClick="btnAdd_Click" Text="Add" />
- </form>
- </body>
- </html>
Default.aspx.cs
Note: Add these namespaces
- using System.Net;
- using System.IO;
- using System.Runtime.Serialization;
- protected void btnAdd_Click(object sender, EventArgs e)
- {
- WebClient proxy = new WebClient();
- byte[] abc = proxy.DownloadData((new Uri("http://localhost:21269/Service1.svc/Add/" + TextBox1.Text + "/" + TextBox2.Text)));
- Stream strm = new MemoryStream(abc);
- DataContractSerializer obj = new DataContractSerializer(typeof(string));
- string result = obj.ReadObject(strm).ToString();
- Response.Write(result);
- }
Thanks...