Overview
This tutorial will show how easy it is to build web applications using .NET 7.0, C# and ASP.NET Razor Pages. You will develop a web app that calculates the future value on an investment based on a monthly contribution that you can make at a given interest rate for a number of years.
Source Code: https://github.com/medhatelmasry/SuperWeb
Companion Video: https://youtu.be/Zzu-WlkhnH8
Getting Started
To display different types of .NET applications that you can build, execute the following command in a terminal window on mac, Linux, or windows:
dotnet new --list
We want to create an ASP.NET Razor Pages application. Go to a working directory and run the following command from a terminal window to create an app in a folder named SuperWeb.
dotnet new razor -f net7.0 -o SuperWeb
OR
dotnet new webapp -f net7.0 -o SuperWeb
Change directory to the newly created folder with:
cd SuperWeb
To run the application in developer mode, run the following command:
dotnet watch
The web app will open in your default browser.
using Microsoft.AspNetCore.Mvc;using Microsoft.AspNetCore.Mvc.RazorPages;namespace SuperWeb.Pages;public class IndexModel : PageModel{private readonly ILogger<IndexModel> _logger;public IndexModel(ILogger<IndexModel> logger){_logger = logger;}public void OnGet(){}}
ViewData["Name"] = "Queen Elizabeth";
namespace SuperWeb.Models;public class FutureValue{public decimal MonthlyInvestment { get; set; }public decimal YearlyInterestRate { get; set; }public int Years { get; set; }public decimal CalculateFutureValue(){int months = Years * 12;decimal monthlyInterestRate =YearlyInterestRate / 12 / 100;decimal futureValue = 0;for (int i = 0; i < months; i++){futureValue = (futureValue + MonthlyInvestment)* (1 + monthlyInterestRate);}return futureValue;}}
using Microsoft.AspNetCore.Mvc.RazorPages;using SuperWeb.Models;namespace SuperWeb.Pages{public class FutureValueModel : PageModel{public decimal MonthlyInvestment { get; set; }public decimal YearlyInterestRate { get; set; }public int Years { get; set; }public void OnGet(){ViewData["FV"] = 0;}public void OnPost(FutureValue model){ViewData["FV"] = model.CalculateFutureValue();}}}
@page@model SuperWeb.Pages.FutureValueModel@{ViewData["Title"] = "Future Value Page";}<div><form method="post"><div><label asp-for="MonthlyInvestment">Monthly Investment:</label><input asp-for="MonthlyInvestment" /></div><div><label asp-for="YearlyInterestRate">Yearly Interest Rate:</label><input asp-for="YearlyInterestRate" /></div><div><label asp-for="Years">Number of Years:</label><input asp-for="Years" /></div><div><label>Future Value:</label><input value='@ViewBag.FV.ToString("C2")' readonly></div><button type="submit">Calculate</button><a asp-action="Index">Clear</a></form></div>
using System.ComponentModel.DataAnnotations;using Microsoft.AspNetCore.Mvc.RazorPages;using SuperWeb.Models;namespace SuperWeb.Pages{public class FutureValueModel : PageModel{[Display(Name="Monthly Investment")]public decimal MonthlyInvestment { get; set; }[Display(Name="Yearly Interest Rate")]public decimal YearlyInterestRate { get; set; }[Display(Name="Number of years")]public int Years { get; set; }public void OnGet(){ViewData["FV"] = 0;}public void OnPost(FutureValue model){ViewData["FV"] = model.CalculateFutureValue();}}}
No comments:
Post a Comment