|
| 1 | +using System; |
| 2 | +using System.Threading.Tasks; |
| 3 | +using Microsoft.AspNetCore.Http; |
| 4 | +using Microsoft.AspNetCore.Routing; |
| 5 | +using Microsoft.AspNetCore.Routing.Patterns; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.Logging; |
| 8 | +using Microsoft.Extensions.Logging.Abstractions; |
| 9 | +using Microsoft.Extensions.Options; |
| 10 | +using Moq; |
| 11 | + |
| 12 | +namespace Q133689 |
| 13 | +{ |
| 14 | + class Program |
| 15 | + { |
| 16 | + static async Task Main(string[] args) |
| 17 | + { |
| 18 | + var requestPath = "/cmt/p/14408628.html"; |
| 19 | + |
| 20 | + var routeTemplate = "/{blogApp}/{postType}/{idOrSlug}.html"; |
| 21 | + var routeHandler = new RouteHandler((context) => null); |
| 22 | + |
| 23 | + IServiceCollection services = new ServiceCollection(); |
| 24 | + services.AddSingleton<ILoggerFactory>(NullLoggerFactory.Instance); |
| 25 | + services.AddOptions<RouteOptions>(); |
| 26 | + var sp = services.BuildServiceProvider(); |
| 27 | + |
| 28 | + var routeOptions = sp.GetRequiredService<IOptions<RouteOptions>>(); |
| 29 | + var inlineConstraintResolver = new DefaultInlineConstraintResolver(routeOptions, sp); |
| 30 | + var route = new Route( |
| 31 | + routeHandler, |
| 32 | + routeTemplate, |
| 33 | + defaults: null, |
| 34 | + constraints: new RouteValueDictionary(), |
| 35 | + dataTokens: null, |
| 36 | + inlineConstraintResolver: inlineConstraintResolver); |
| 37 | + |
| 38 | + var httpContext = new DefaultHttpContext(); |
| 39 | + httpContext.Request.Path = new PathString(requestPath); |
| 40 | + httpContext.RequestServices = sp; |
| 41 | + |
| 42 | + var routeContext = new RouteContext(httpContext); |
| 43 | + await route.RouteAsync(routeContext); |
| 44 | + |
| 45 | + Console.WriteLine("blogApp: " + routeContext.RouteData.Values["blogApp"]); |
| 46 | + Console.WriteLine("postType:" + routeContext.RouteData.Values["postType"]); |
| 47 | + Console.WriteLine("idOrSlug:" + routeContext.RouteData.Values["idOrSlug"]); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments