concept route template in category asp.net

appears as: route template, A route template, route templates, route templates, The route template, route template
ASP.NET Core in Action, Second Edition MEAP V04

This is an excerpt from Manning's book ASP.NET Core in Action, Second Edition MEAP V04.

 A route template is a URL pattern that is used to match against request URLs. They’re strings of fixed values, like "/test" in the previous listing. They can also contain placeholders for variables, as you’ll see in section 5.3.

Figure 5.3 Endpoint routing uses a two-step process. The RoutingMiddleware selects which endpoint to execute, and the EndpointMiddleware executes it. If the request URL doesn’t match a route template, the endpoint middleware will not generate a response.

If the request URL doesn’t match a route template, then the RoutingMiddleware doesn’t select an endpoint, but the request still continues down the middleware pipeline. As no endpoint is selected, the EndpointMiddleware silently ignores the request, and passes it to the next middleware in the pipeline. The EndpointMiddleware is typically the final middleware in the pipeline, so the “next” middleware is normally the “dummy” middleware that always returns a 404 Not Found response, as you saw in chapter 3.

As I mentioned in the section 5.2.2, Razor Pages uses attribute routing by creating route templates based on conventions. ASP.NET Core creates a route template for every Razor Page in your app during app startup, when you call MapRazorPages in the Configure method of Startup.cs:

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
});

For every Razor Page in your application, the framework uses the path of the Razor Page file relative to the Razor Pages root directory (Pages/), excluding the file extension (cshtml). For example, if you have a Razor Page located at the path Pages/Products/View.cshtml, the framework creates a route template with the value "Products/View", as shown in figure 5.6.

ASP.NET Core in Action

This is an excerpt from Manning's book ASP.NET Core in Action.

Whichever technique you use, you’ll define your expected URLs using route templates. These define the pattern of the URL you’re expecting, with placeholders for parts that may vary.

Listing 5.2. Adding multiple routes to the MvcMiddleware

When you define a route during the configuration of the MvcMiddleware, you specify a route template that defines the URL pattern the route will match. The route template has a rich, flexible syntax, but a simple example is shown in figure 5.5.

Figure 5.5. A simple route template showing a literal segment and required route parameters

A router parses a route template by splitting it into a number of segments. A segment is typically separated by the / character, but it can be any valid character. Each segment is either

  • Route templates define the structure of known URLs in your application. They’re strings with placeholders for variables that can contain optional values and map to controllers and actions.
  • sitemap

    Unable to load book!

    The book could not be loaded.

    (try again in a couple of minutes)

    manning.com homepage
    test yourself with a liveTest