liveBook · Manning

Cshtml file extension

As explained in Pro ASP.NET Core 7, Tenth Edition

The .cshtml file extension identifies files containing Razor markup and embedded C# code.

The .cshtml extension identifies Razor files that combine HTML elements with embedded C# code. Razor views use these files to generate response HTML from controller-provided model data. section 3.2.8

Use the extension in two closely related ASP.NET Core patterns:

  • Name a controller view after its action, such as ListResponses.cshtml in Views/Home; the view renders the model passed by View. section 3.2.8
  • Create Razor Pages such as Pages/Index.cshtml; add @page first, select a page model with @model, and define supporting code with @functions. section 23.2.2
  • Keep the distinction clear: the same extension does not determine the hosting model; @page prevents a Razor Page from being treated as a controller view. section 23.2.2

Rendering filtered responses

A ListResponses.cshtml view declares @model IEnumerable<PartyInvites.Models.GuestResponse> and uses @foreach to turn each response into a table row. The controller supplies only guests whose WillAttend value is true. section 3.2.8

Listing 3.16 Displaying data in the ListResponses.cshtml file in the Views/Home folder
@model IEnumerable<PartyInvites.Models.GuestResponse>
@{
   Layout = null;
}

<!DOCTYPE html>

<html>
<head>
   <meta name="viewport" content="width=device-width" />
   <title>Responses</title>
</head>
<body>
   <h2>Here is the list of people attending the party</h2>
   <table>
       <thead>
           <tr><th>Name</th><th>Email</th><th>Phone</th></tr>
       </thead>
       <tbody>
           @foreach (PartyInvites.Models.GuestResponse r in Model!) {
               <tr>
                   <td>@r.Name</td>
                   <td>@r.Email</td>
                   <td>@r.Phone</td>
               </tr>
           }
       </tbody>
   </table>
</body>
</html>

Inspect a complete controller-view file showing model declaration, Razor iteration, HTML output, and property expressions. section 3.2.8

Listing 23.4 The contents of the Index.cshtml file in the Pages folder
@page
@model IndexModel
@using Microsoft.AspNetCore.Mvc.RazorPages
@using WebApp.Models;

<!DOCTYPE html>
<html>
<head>
   <link href="/lib/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
   <div class="bg-primary text-white text-center m-2 p-2">
       @Model.Product?.Name
   </div>
</body>
</html>

@functions {

   public class IndexModel : PageModel {
       private DataContext context;
               
       public Product? Product { get; set; }
               
       public IndexModel(DataContext ctx) {
           context = ctx;
       }
               
       public async Task OnGetAsync(long id = 1) {
           Product = await context.Products.FindAsync(id);
       }
   }
}

Compare a Razor Page file that combines @page, @model, HTML markup, and an inline PageModel implementation. section 23.2.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains markup mixing, model directives, page directives, file-based routing, and inline page-model code without detailing compilation internals.
Examples
Uses ListResponses.cshtml and Pages/Index.cshtml to demonstrate response tables, product output, directives, and page-model handlers.
What it assumes
Assumes basic ASP.NET Core structure and C# while teaching file placement, Razor directives, model selection, and view-page differences.
What you can do after
Builds Razor view and page files that combine HTML with C#, receive model data, and produce browser responses.
How it connects
Connects Razor markup and C# with MVC views, Razor Pages, page models, model binding, and URL routing.

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.