liveBook · Manning

HTTP GET

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

An HTTP GET request is an HTTP request that retrieves a resource or representation without requesting a state-changing operation.

An HTTP GET request asks for a resource, such as a page or a form, rather than requesting a state-changing operation. A browser normally sends one when someone follows a link. In ASP.NET Core, the request method also determines which controller action or Razor Page handler runs. section 3.2.6 section 23.4.3

Keep the work done on a GET separate from form submission:

  • In a controller, mark the action that displays the blank form with [HttpGet]. A separate [HttpPost] action at the same URL receives the submitted form data. section 3.2.6
  • In a Razor Page, OnGetAsync can load data for display while OnPostAsync handles the submitted change. After POST, redirecting to the page makes the browser send a GET for the updated view; reloading then does not repeat the POST. section 23.4.3 section 27.2
  • A form button can deliberately use GET for navigation. Its values travel in the query string, so use it to carry the details needed to open another page, not to perform the creation itself. section 31.4.2

Edit a product

Open http://localhost:5000/editor/1: OnGetAsync finds the product and displays its details. Change the price to 100 and submit the form: OnPostAsync saves the change, then RedirectToPage causes a fresh GET that displays the updated data. section 23.4.3

Listing 3.11 Adding a method in the HomeController.cs file in the Controllers folder
using Microsoft.AspNetCore.Mvc;
using PartyInvites.Models;

namespace PartyInvites.Controllers {
    public class HomeController : Controller {
        
        public IActionResult Index() {
            return View();
        }
 
        [HttpGet]
        public ViewResult RsvpForm() {
            return View();
        }
 
        [HttpPost]
        public ViewResult RsvpForm(GuestResponse guestResponse) {
            // TODO: store response from guest
            return View();
        }
    }
}

The HomeController listing puts [HttpGet] and [HttpPost] on two RsvpForm actions. The GET action returns the form view; the POST action accepts a GuestResponse. section 3.2.6

Listing 31.31 Adding elements in the _ProductEditor.cshtml file in the Pages folder
...
<div class="form-group">
    <label asp-for="Product.SupplierId">
        Supplier
        @if (Model?.ReadOnly == false) {
            <input type="hidden" name="returnPage" 
                value="@Model?.Action" />
            <button class="btn btn-sm btn-outline-primary ml-3 my-1"
                asp-page="SupplierBreakOut" formmethod="get" 
                    formnovalidate>
                Create New Supplier
            </button>
        }
    </label>
    <div>
        <span asp-validation-for="Product.SupplierId" 
            class="text-danger">
        </span>
    </div>
    <select asp-for="Product.SupplierId" class="form-control"
            disabled="@Model?.ReadOnly"
            asp-items="@(new SelectList(Model?.Suppliers,
                "SupplierId", "Name"))">
        <option value="" disabled selected>Choose a Supplier</option>
    </select>
</div>
...

The Create New Supplier button uses formmethod="get" to navigate to the breakout page with the current form values in the query string. section 31.4.2

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

How deep it goes
Works through controller attributes, Razor Page handlers, redirects, and GET form navigation, without detailing HTTP protocol internals.
Examples
Shows an RSVP form, a product editor at /editor/1, and a button that opens a supplier-creation page.
What it assumes
Introduces GET as ordinary browser link navigation while assuming familiarity with C# application code and HTML forms.
What you can do after
Build it from scratch: route a GET to a display action or handler and use a redirect to display results after POST.
How it connects
Connects HTTP GET to HTTP POST, controller actions, Razor Page handlers, query strings, and Post/Redirect/Get.

Also explained in

Compare how 14 books explain HTTP GET →

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.