ASP.NET Core Sample Website - Calculator

In this article, I will be explaining how to create a simple Web Calculator using .NET Core. This article does not set up the database but if you would like to create one, here are some examples,
Some useful tips when creating your database,
How to publish your .NET Core Application to Azure,
What is ASP.NET Core and why do we use it?
  • Easy to develop Web Pages as well as Web APIs.
  • Integration of modern frameworks and development workflows.
  • Easy to integrate with Azure.
  • Natural dependency injection support.
  • Large hosting variety, like on IIS, Apache, Docker, or self-host in your own process.
  • A wide range of tools that simplifies web development.
  • Multi-platform, so you may build and run on Windows, macOS, and Linux.
  • Open-source and with a powerful community.
We are using a single controller with two Action Results - one for the first HttpGet and another to calculate the operation on the server side. Here is the code. 
  1. public class HomeController : Controller  
  2. {  
  3.     [HttpGet]  
  4.     public IActionResult Index()  
  5.     {  
  6.         return View();  
  7.     }  
  8.   
  9.     [HttpPost]  
  10.     public IActionResult Index( Operation model )  
  11.     {  
  12.         if ( model.OperationType == OperationType.Addition )  
  13.             model.Result = model.NumberA + model.NumberB;  
  14.         return View( model );  
  15.     }  
  16. }  
This is the View,
  1. @model Operation  
  2. <form asp-controller="Home" asp-action="Index" method="post" >  
  3.     <div class="form-group">  
  4.         <div class="row">  
  5.             <label asp-for="NumberA" class="col-lg-2"></label>  
  6.             <input type="number" asp-for="NumberA" class="col-lg-2" />  
  7.         </div>  
  8.         <div class="row">  
  9.             <label asp-for="NumberB" class="col-lg-2"></label>  
  10.             <input type="number" asp-for="NumberB" class="col-lg-2" />  
  11.         </div>  
  12.         <div class="row">  
  13.             <label asp-for="OperationType" class="col-lg-2"></label>  
  14.             <select asp-for="OperationType" class="col-lg-2" asp-items="Html.GetEnumSelectList<OperationType>()">  
  15.                 <option selected="selected" value="">Select</option>  
  16.             </select>  
  17.         </div>  
  18.         <div class="row">  
  19.             <label asp-for="Result" class="col-lg-2"></label>  
  20.             <input type="number" disabled="disabled" class="col-lg-2" asp-for="Result" />  
  21.         </div>  
  22.         <div class="row">  
  23.             <input type="submit" value="Submit" asp-action="Index" />  
  24.         </div>  
  25.     </div>  
  26. </form>  
View Result in the browser.
 
ASP.NET Core Sample Website - Calculator 
This is the model used in here.
  1. public class Operation  
  2. {  
  3.     [Display( Name = "First Number" )]  
  4.     public double NumberA { getset; }  
  5.   
  6.     [Display( Name = "Second Number" )]  
  7.     public double NumberB { getset; }  
  8.   
  9.     [Display( Name = "Result" )]  
  10.     public double Result { getset; }  
  11.   
  12.     [Display( Name = "Operation" )]  
  13.     public OperationType OperationType { getset; }  
  14. }  
  1. public enum OperationType  
  2. {  
  3.     Addition,  
  4.     Multiplication,  
  5.     Division,  
  6.     Subtraction  
  7. }  
Right now, only addition is implemented.
 
ASP.NET Core Sample Website - Calculator
 
You can implement multiplication, division, and subtraction as well. I will publish these methods in my next article.
 
Congratulations, you have successfully created your Web Calculator using .NET Core. 
 
You may find more official material regarding ASP.NET Core here.