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.
An example. 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.
Info We add a second 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.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.