MapGet
When we create an ASP.NET Core Web Application, we must specify a lambda in the UseEndpoints
method in Configure
. MapGet
calls are often used here.
With this method, we map a pattern to a method call. When the page is accessed in a browser, we can then invoke the method and dynamically generate the page HTML.
Here we modify the Configure
method in an ASP.NET Core Web Application. Specify that you want to create an Empty web application in Visual Studio, and then edit Startup.
MapGet
call after the first, and specify that it handles the special URL "bird" for a page about birds.using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Hosting; namespace WebApplication3 // Date: 3/23/2020. { public class Startup { public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseEndpoints(endpoints => { // First MapGet call. endpoints.MapGet("/", async context => { await context.Response.WriteAsync("Home"); }); // Add a second MapGet call to the endpoints lambda. endpoints.MapGet("/bird", async context => { await context.Response.WriteAsync(GetBirdPage()); }); }); } string GetBirdPage() { // Get HTML for bird page. return "Bird page"; } } }Visit /bird: Bird page
MapGet
in UseEndpoints
We can add multiple MapGet
calls in UseEndpoints
—the calls to MapGet
are enclosed in an anonymous function body. This is compiled into an Action instance.
By calling MapGet
in the lambda expression argument to UseEndpoints
, we can specify multiple page URLs. These are resolved by calling asynchronous methods in ASP.NET Core.