liveBook · Manning

Handler

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

A callable operation that responds to an occurrence such as a user interaction or program event.

A handler is a method invoked in response to a request or event. In ASP.NET Core, the name matters: a Razor Page uses page-model handlers such as OnGet and OnPost for HTTP requests, while a Blazor component connects an event such as a button click to a method with an @onclick attribute. section 23.2.2 section 33.4.1

For a Razor Page, follow the request from route to response:

  • The route selects the page; the framework creates its page model and resolves constructor dependencies. OnGet handles a GET request, and OnGetAsync is its asynchronous counterpart. section 23.2.2 section 23.3
  • A form POST invokes OnPost or OnPostAsync. Model binding supplies handler parameters from the request, including route values and form fields. A handler can update data and return a redirect rather than render the page immediately. section 23.4.3 section 8.2.5
  • A page can have named alternatives. For example, asp-page-handler="Remove" on a POST form selects OnPostRemove; a handler query-string value of related selects OnGetRelatedAsync without the OnGet prefix or Async suffix. section 9.2.1 section 23.4.4

Edit a product price

An editor page loads a product in OnGetAsync and displays its price in a form. Submission sends a POST to the page’s URL; OnPostAsync receives the route id and submitted price, saves the change, then calls RedirectToPage(). The form includes an anti-forgery token: without the required field, the POST is rejected. section 23.4.3

Listing 23.17 The contents of the Editor.cshtml.cs file in the Pages folder
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WebApp.Models;

namespace WebApp.Pages {
   public class EditorModel : PageModel {
       private DataContext context;
               
       public Product? Product { get; set; }
               
       public EditorModel(DataContext ctx) {
           context = ctx;
       }
               
       public async Task OnGetAsync(long id) {
           Product = await context.Products.FindAsync(id);
       }
               
       public async Task<IActionResult> OnPostAsync(long id, 
               decimal price) {
           Product? p = await context.Products.FindAsync(id);
           if (p != null) {
               p.Price = price;
           }
           await context.SaveChangesAsync();
           return RedirectToPage();
       }
   }
}

Compare the two page-model method signatures, their database operations, and the redirect returned after the POST. section 23.4.3

Blazor event handlers respond to events in a component rather than to a page’s HTTP method. With @onclick="IncrementCounter", a button click invokes the named method, which may receive MouseEventArgs. In the Blazor Server example, JavaScript forwards the event to the server; a changed component value is then reflected in the browser. Use a lambda when the handler also needs a value identifying which generated button was clicked. section 33.4.1

Listing 33.10 The contents of the Events.razor file in the Blazor folder
<div class="m-2 p-2 border">
   <button class="btn btn-primary" @onclick="IncrementCounter">
       Increment
   </button>
   <span class="p-2">Counter Value: @Counter</span>
</div>

@code {
   public int Counter { get; set; } = 1;
       
   public void IncrementCounter(MouseEventArgs e) {
       Counter++;
   }
}

The button attribute names the click handler; the method increments the value displayed beside it. section 33.4.1

Do not mistake a visible browser control for a working handler. An uncaught Blazor Server exception can leave controls usable in the browser while later event handlers stop responding; an error UI element can make that failure visible and offer a reload. section 34.4.2

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

How deep it goes
Works through handler naming, selection, parameter binding, redirects, and server-side event dispatch; defers detailed model binding to another treatment.
Examples
Uses a cart add/remove flow, a product-price editor, and Blazor counter buttons to show handlers in action.
What it assumes
Introduces page models and event registration while using C#, Razor markup, HTML forms, and HTTP GET/POST requests.
What you can do after
Build it from scratch: implement GET, POST, and named Razor Page handlers, then wire a Blazor button to a component method.
How it connects
Connects handlers to Razor Pages routing, model binding, HTML forms, anti-forgery tokens, and Blazor component events.

Also explained in

Compare how 22 books explain handler →

Related concepts

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.