// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Dispatcher; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing.Dispatcher; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace DispatcherSample { public class Startup { private readonly static IInlineConstraintResolver ConstraintResolver = new DefaultInlineConstraintResolver( new OptionsManager( new OptionsFactory( Enumerable.Empty>(), Enumerable.Empty>()))); public void ConfigureServices(IServiceCollection services) { services.Configure(options => { options.Dispatchers.Add(new TreeDispatcher() { Addresses = { new TemplateAddress("{controller=Home}/{action=Index}/{id?}", new { controller = "Home", action = "Index", }, "Home:Index()"), new TemplateAddress("{controller=Home}/{action=Index}/{id?}", new { controller = "Home", action = "About", }, "Home:About()"), new TemplateAddress("{controller=Home}/{action=Index}/{id?}", new { controller = "Admin", action = "Index", }, "Admin:Index()"), new TemplateAddress("{controller=Home}/{action=Index}/{id?}", new { controller = "Admin", action = "Users", }, "Admin:GetUsers()/Admin:EditUsers()"), }, Endpoints = { new TemplateEndpoint("{controller=Home}/{action=Index}/{id?}", new { controller = "Home", action = "Index", }, Home_Index, "Home:Index()"), new TemplateEndpoint("{controller=Home}/{action=Index}/{id?}", new { controller = "Home", action = "About", }, Home_About, "Home:About()"), new TemplateEndpoint("{controller=Home}/{action=Index}/{id?}", new { controller = "Admin", action = "Index", }, Admin_Index, "Admin:Index()"), new TemplateEndpoint("{controller=Home}/{action=Index}/{id?}", new { controller = "Admin", action = "Users", }, "GET", Admin_GetUsers, "Admin:GetUsers()", new AuthorizationPolicyMetadata("Admin")), new TemplateEndpoint("{controller=Home}/{action=Index}/{id?}", new { controller = "Admin", action = "Users", }, "POST", Admin_EditUsers, "Admin:EditUsers()", new AuthorizationPolicyMetadata("Admin")), }, Selectors = { new TemplateEndpointSelector(), new HttpMethodEndpointSelector(), } }); options.HandlerFactories.Add((endpoint) => (endpoint as TemplateEndpoint)?.HandlerFactory); }); services.AddDispatcher(); services.AddRouting(); services.AddSingleton(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILogger logger) { app.UseDispatcher(); app.Use(async (context, next) => { logger.LogInformation("Executing fake CORS middleware"); var feature = context.Features.Get(); var policy = feature.Endpoint?.Metadata.OfType().LastOrDefault(); logger.LogInformation("using CORS policy {PolicyName}", policy?.Name ?? "default"); await next.Invoke(); }); app.Use(async (context, next) => { logger.LogInformation("Executing fake AuthZ middleware"); var feature = context.Features.Get(); var policy = feature.Endpoint?.Metadata.OfType().LastOrDefault(); if (policy != null) { logger.LogInformation("using Auth policy {PolicyName}", policy.Name); } await next.Invoke(); }); } public static Task Home_Index(HttpContext httpContext) { var url = httpContext.RequestServices.GetService(); return httpContext.Response.WriteAsync( $"" + $"" + $"

Some links you can visit

" + $"

Home:Index()

" + $"

Home:About()

" + $"

Admin:Index()

" + $"

Admin:GetUsers()/Admin:EditUsers()

" + $"" + $""); } public static Task Home_About(HttpContext httpContext) { return httpContext.Response.WriteAsync( $"" + $"" + $"

This is a dispatcher sample.

" + $"" + $""); } public static Task Admin_Index(HttpContext httpContext) { return httpContext.Response.WriteAsync( $"" + $"" + $"

This is the admin page.

" + $"" + $""); } public static Task Admin_GetUsers(HttpContext httpContext) { return httpContext.Response.WriteAsync( $"" + $"" + $"

Users: rynowak, jbagga

" + $"" + $""); } public static Task Admin_EditUsers(HttpContext httpContext) { return httpContext.Response.WriteAsync( $"" + $"" + $"

blerp

" + $"" + $""); } } }