Skip to main content

Command Palette

Search for a command to run...

DotNet Web MVC Part 3

Published
1 min read
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

[1] Add a view

Add an Index view for the HelloWorldController:

  • Add a new folder named Views/HelloWorld.

  • Add a new file to the Views/HelloWorld folder, and name it Index.cshtml.

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>Hello from our View Template!</p>

[2] Update Controller

Replace the Index() method with the following:

    public IActionResult Index()
    {
        return View();
    }

Full code:

using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;

namespace MvcMovie.Controllers;

public class HelloWorldController : Controller
{
    // 
    // GET: /HelloWorld/
    public IActionResult Index()
    {
        return View();
    }
    // 
    // GET: /HelloWorld/Welcome/ 
    public string Welcome(string name, int numTimes = 1)
    {
        return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}");
    }
}

Output:

REFERENCE:

https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-view?view=aspnetcore-8.0&tabs=visual-studio-code

DotNet Web MVC Part 3