Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HelloMiddleware.cs « HelloExtension « RoutingWebSite « testassets « test « Routing « Http « src - github.com/dotnet/aspnetcore.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2494ae7ea96c4c440e77502c405053577150a488 (plain)
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
37
38
39
40
41
42
43
44
45
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Options;

namespace RoutingWebSite.HelloExtension
{
    public class HelloMiddleware
    {
        private readonly RequestDelegate _next;
        private readonly HelloOptions _helloOptions;
        private readonly byte[] _helloPayload;

        public HelloMiddleware(RequestDelegate next, IOptions<HelloOptions> helloOptions)
        {
            _next = next;
            _helloOptions = helloOptions.Value;

            var payload = new List<byte>();
            payload.AddRange(Encoding.UTF8.GetBytes("Hello"));
            if (!string.IsNullOrEmpty(_helloOptions.Greeter))
            {
                payload.Add((byte)' ');
                payload.AddRange(Encoding.UTF8.GetBytes(_helloOptions.Greeter));
            }
            _helloPayload = payload.ToArray();
        }

        public Task InvokeAsync(HttpContext context)
        {
            var response = context.Response;
            var payloadLength = _helloPayload.Length;
            response.StatusCode = 200;
            response.ContentType = "text/plain";
            response.ContentLength = payloadLength;
            return response.Body.WriteAsync(_helloPayload, 0, payloadLength);
        }
    }
}