using System; using System.Net; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using StartupFilterSample.Models; namespace StartupFilterSample { #region snippet1 public class RequestSetOptionsMiddleware { private readonly RequestDelegate _next; private IOptions _injectedOptions; public RequestSetOptionsMiddleware( RequestDelegate next, IOptions injectedOptions) { _next = next; _injectedOptions = injectedOptions; } public async Task Invoke(HttpContext httpContext) { Console.WriteLine("RequestSetOptionsMiddleware.Invoke"); var option = httpContext.Request.Query["option"]; if (!string.IsNullOrWhiteSpace(option)) { _injectedOptions.Value.Option = WebUtility.HtmlEncode(option); } await _next(httpContext); } } #endregion }