-
Open NuGet Package Manager
-
Add these packages:
- System.ServiceModel.Http
- System.ServiceModel.Primitives
-
Open the project properties
-
In the Startup Project select
Multiple startup projects
-
Set booth projects action to
Start
-
Create a new Solution:
ASP.NET Web Application (.NET Framework)
-
Add Web Service (ASMX)
-
Add the new
WebMethod
to the service:- Add(int a, int b): int
- Sub(int a, int b): int
- Mul(int a, int b): int
- Div(int a, int b): double
-
Set the WebService as start page
-
Create a new Solution:
ASP.NET Core Web App (MVC)
-
Add Connected Services
-
Add Microsoft WCF Web Service Reference Provider
-
As End-Point add
http://localhost:51902/CalculatorService.asmx
-
In Client Option set: Generate Synchronous Operations
-
Create an Model:
namespace CalculatorClient.Models
{
public class CalculatorFields
{
public int Avalue { get; set; }
public int Bvalue { get; set; }
public double Result { get; set; }
}
}
- Add the Model and Form to the View index.cshtml:
@model CalculatorClient.Models.CalculatorFields
@{
ViewData["Title"] = "Calculator";
}
<div class="text-center">
<h1 class="display-4">Calculator</h1>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Person Details</th>
</tr>
<tr>
<td>Value A: </td>
<td>
@Html.TextBoxFor(m => m.Avalue)
</td>
</tr>
<tr>
<td>Value B: </td>
<td>
@Html.TextBoxFor(m => m.Bvalue)
</td>
</tr>
<tr>
<td>Result: </td>
<td>
@Html.DisplayFor(m => m.Result)
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" name="submitButton" value="Add" />
<input type="submit" name="submitButton" value="Subtract" />
<input type="submit" name="submitButton" value="Multiply" />
<input type="submit" name="submitButton" value="Divide" />
</td>
</tr>
</table>
}
</div>
- Add the Post Method to the Controller HomeController.cs:
[HttpPost]
public ActionResult Index(CalculatorFields fields, string submitButton)
{
switch (submitButton)
{
case "Add":
fields.Result = client.Add(fields.Avalue, fields.Bvalue);
break;
case "Subtract":
fields.Result = client.Sub(fields.Avalue, fields.Bvalue);
break;
case "Multiply":
fields.Result = client.Mul(fields.Avalue, fields.Bvalue);
break;
case "Divide":
fields.Result = client.Div(fields.Avalue, fields.Bvalue);
break;
default:
fields = new CalculatorFields() { Avalue = 0, Bvalue = 0, Result = 0 };
break;
}
return View(fields);
}