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 pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.