bashCopy code
dotnet new webapp -n SimpleCalculator
bashCopy code
cd SimpleCalculator
bashCopy code
code .
bashCopy code
dotnet run
bashCopy code
dotnet new webapp -n SimpleCalculator
bashCopy code
cd SimpleCalculator
htmlCopy code
@page
@model IndexModel
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<form method="post">
<input type="number" name="num1" required />
<select name="operation">
<option value="add">+</option>
<option value="subtract">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
<input type="number" name="num2" required />
<button type="submit">Calculate</button>
</form>
@if(Model.Result.HasValue)
{
<h2>Result: @Model.Result</h2>
}
</body>
</html>
csharpCopy code
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc;
namespace SimpleCalculator.Pages
{
public class IndexModel : PageModel
{
[BindProperty]
public double Num1 { get; set; }
[BindProperty]
public double Num2 { get; set; }
[BindProperty]
public string Operation { get; set; }
public double? Result { get; set; }
public void OnPost()
{
switch (Operation)
{
case "add":
Result = Num1 + Num2;
break;
case "subtract":
Result = Num1 - Num2;
break;
case "multiply":
Result = Num1 * Num2;
break;
case "divide":
if (Num2 != 0)
{
Result = Num1 / Num2;
}
else
{
// Handle division by zero
Result = null;
}
break;
}
}
}
}
bashCopy code
dotnet run