Keep in Touch!

Contenuti

MapExtensions.Map: Pipeline Custom in .NET Core

Utilizzare MapExtensions.Map è davvero semplice e dai risvolti potenti. Grazie ad esso -specialmente realizzando un middleware in .NETCore possiamo creare delle “mappe” tra la richiesta in ingresso e l’output fornito.

MapExtensions.Map: Definizione

Andando sulla documentazione ufficiale .NETCore possiamo leggere quanto segue

MapExtensions.Map(IApplicationBuilder, PathString, Action) Namespace: Microsoft.AspNetCore.Builder Assembly: Microsoft.AspNetCore.Http.Abstractions.dll

Branches the request pipeline based on matches of the given request path. If the request path starts with the given path, the branch is executed.

MapExtensions.Map: HowTo

Per utilizzare “.Map” dobbiamo aprire il file Startup.cs presente all’interno del nostro progetto NET Core ed aggiungere le seguente righe.

*Startup.cs - Configure*
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
[...]
app.Map("/CreatedBy", (applicationBuilder) =>
{
	applicationBuilder.Run(async (context) =>
	{
		await context.Response.WriteAsync("Created By DevAndreaCarratta.IT");	
	});
});
[...]
}

Ora, per fare la prova, non vi resterà che chiamare il vostro middleware (o sito .NETCore) ed aggiungere “/CreatedBy” alla root del sito.

A video apparirà la scritta

Created By DevAndreaCarratta.IT

definita all’interno del file Startup.cs poche righe prima.

MapExtensions: definizione classe / metodo

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#region Assembly Microsoft.AspNetCore.Http.Abstractions, Version=2.2.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http.abstractions\2.2.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll
#endregion

using System;
using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Builder
{
    //
    // Summary:
    //     Extension methods for the Microsoft.AspNetCore.Builder.Extensions.MapMiddleware.
    public static class MapExtensions
    {
        //
        // Summary:
        //     Branches the request pipeline based on matches of the given request path. If
        //     the request path starts with the given path, the branch is executed.
        //
        // Parameters:
        //   app:
        //     The Microsoft.AspNetCore.Builder.IApplicationBuilder instance.
        //
        //   pathMatch:
        //     The request path to match.
        //
        //   configuration:
        //     The branch to take for positive path matches.
        //
        // Returns:
        //     The Microsoft.AspNetCore.Builder.IApplicationBuilder instance.
        public static IApplicationBuilder Map(this IApplicationBuilder app, PathString pathMatch, Action<IApplicationBuilder> configuration);
    }
}

Mapextensions.Map: Riferimenti

Docs Microsoft: MapExtensions.Map(IApplicationBuilder, PathString, Action) Method