In this lab, you’ll take a simple C# WinForms desktop application and use it as the basis to develop a Razor MVC web application in .NET 8. The web app will have the same features as the desktop version:
By the end, you will have a working Razor MVC web app called DesktopToRazor.
DesktopToRazor and choose a folder location. Use the following options when setting up the new project:Your MVC project is now set up.
From your desktop project, you already have three logic classes: Customer.cs, Calculator.cs, and CustomerHelper.cs. We will reuse them in the web app.
Customer.cs with this code:namespace DesktopToRazor.Models
{
public class Customer
{
public string Name { get; set; }
public int Age { get; set; }
}
}
Calculator.cs in the same folder:namespace DesktopToRazor.Models
{
public class Calculator
{
public int DoubleAge(int age)
{
return age * 2;
}
}
}
CustomerHelper.cs in the same folder:namespace DesktopToRazor.Models
{
public class CustomerHelper
{
public string GetCustomerInfo(Customer customer)
{
return $"Customer: {customer.Name}, Age: {customer.Age}";
}
}
}
Controllers handle user actions and decide what to show. We’ll create a new CustomerController to manage form input and button clicks.
CustomerController.cs.using Microsoft.AspNetCore.Mvc;
using DesktopToRazor.Models;
namespace DesktopToRazor.Controllers
{
public class CustomerController : Controller
{
private static Customer? _customer;
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult CreateCustomer(string name, int age)
{
var customer = new Customer { Name = name, Age = age };
_customer = customer;
var helper = new CustomerHelper();
ViewBag.Message = helper.GetCustomerInfo(customer) + " - Customer Saved!";
return View("Index");
}
[HttpPost]
public IActionResult DoubleAge(int age)
{
var calculator = new Calculator();
int doubled = calculator.DoubleAge(age);
ViewBag.Message = $"Doubled Age: {doubled}";
return View("Index");
}
}
}
The view is the HTML form where the user types in data. We’ll create Index.cshtml for our form.
Customer.Customer, right-click → Add → Razor Component → Razor View - Empty, name it Index.cshtml.@{
ViewData["Title"] = "Customer Form";
}
<h2>Customer Form</h2>
<form asp-action="CreateCustomer" method="post">
<label>Name:</label>
<input type="text" name="name" /><br /><br />
<label>Age:</label>
<input type="number" name="age" /><br /><br />
<button type="submit">Create Customer</button>
</form>
<br />
<form asp-action="DoubleAge" method="post">
<label>Age:</label>
<input type="number" name="age" /><br /><br />
<button type="submit">Double Age</button>
</form>
<br />
<div style="margin-top:20px; color:green;">
@ViewBag.Message
</div>
By default, MVC apps point to HomeController. We want our app to open CustomerController instead.
Program.cs located in the root of Solution Explorer.app.MapControllerRoute line and replace it with:app.MapControllerRoute(
name: "default",
pattern: "{controller=Customer}/{action=Index}/{id?}");
http://localhost:5000 or something similar.
✅ You reused your desktop logic classes (Customer, Calculator, CustomerHelper).
✅ You created a new controller (CustomerController).
✅ You added a Razor view (Index.cshtml).
✅ You updated routing in Program.cs.
Your desktop app is now working as a Razor MVC web app!