← Back to ASP.NET Core Tutorial Home

Lesson 8: Working with Views

Views are the presentation layer of an ASP.NET Core MVC application. They are responsible for displaying HTML content to the user. Most MVC views are written using Razor syntax and stored as .cshtml files.

What a view does

A view displays content such as headings, text, forms, tables, images, and buttons. It can also display data passed from a controller.

Where views are stored

Views are usually stored inside the Views folder. They are commonly grouped by controller name.

Views/
  Home/
    Index.cshtml
    About.cshtml

For example, the Index() action in HomeController usually looks for:

Views/Home/Index.cshtml

A simple view example

@{
    ViewData["Title"] = "Home Page";
}

Welcome to ASP.NET Core

This is my first Razor view.

What is Razor?

Razor is a syntax that allows you to mix HTML with C# code inside a view. The @ symbol is used to switch into C#.

Displaying data in a view

A controller can send data to a view, and the view can show it to the user.

public IActionResult Welcome()
{
    ViewBag.Message = "Hello from the controller";
    return View();
}

Then in the view:

@ViewBag.Message

Views and layout pages

Most views are displayed inside a shared layout. A layout provides the common page structure, such as the header, menu, and footer. This avoids repeating the same HTML on every page.

Good design: Views should focus on displaying content, not on handling complicated business logic.

Conclusion

Views are where users see the results of your application. Once you understand Razor syntax and the Views folder structure, you can start building dynamic user interfaces more confidently.

← Previous Lesson Next Lesson: Models and Data Binding →