示例#1
0
        public void Configure(IApplicationBuilder builder)
        {
            var endpoint1 = new DelegateRouteEndpoint(async (context) =>
                                                        await context
                                                                .HttpContext
                                                                .Response
                                                                .WriteAsync(
                                                                  "match1, route values -" + context.RouteData.Values.Print()));

            var endpoint2 = new DelegateRouteEndpoint(async (context) =>
                                                        await context
                                                                .HttpContext
                                                                .Response
                                                                .WriteAsync("Hello, World!"));

            var routeBuilder = new RouteBuilder();
            routeBuilder.DefaultHandler = endpoint1;
            routeBuilder.ServiceProvider = builder.ApplicationServices;

            routeBuilder.AddPrefixRoute("api/store");

            routeBuilder.MapRoute("defaultRoute",
                                  "api/constraint/{controller}",
                                  null,
                                  new { controller = "my.*" });
            routeBuilder.MapRoute("regexStringRoute",
                                  "api/rconstraint/{controller}",
                                  new { foo = "Bar" },
                                  new { controller = new RegexRouteConstraint("^(my.*)$") });
            routeBuilder.MapRoute("regexRoute",
                                  "api/r2constraint/{controller}",
                                  new { foo = "Bar2" },
                                  new
                                  {
                                      controller = new RegexRouteConstraint(
                                          new Regex("^(my.*)$", RegexOptions.None, TimeSpan.FromSeconds(10)))
                                  });

            routeBuilder.MapRoute("parameterConstraintRoute",
                                  "api/{controller}/{*extra}",
                                  new { controller = "Store" });

            routeBuilder.AddPrefixRoute("hello/world", endpoint2);

            routeBuilder.MapLocaleRoute("en-US", "store/US/{action}", new { controller = "Store" });
            routeBuilder.MapLocaleRoute("en-GB", "store/UK/{action}", new { controller = "Store" });

            routeBuilder.AddPrefixRoute("", endpoint2);

            builder.UseRouter(routeBuilder.Build());
        }
示例#2
0
        public void Configure(IApplicationBuilder app)
        {
            var endpoint1 = new RouteHandler((c) =>
            {
                return c.Response.WriteAsync($"match1, route values - {string.Join(", ", c.GetRouteData().Values)}");
            });

            var endpoint2 = new RouteHandler((c) => c.Response.WriteAsync("Hello, World!"));

            var routeBuilder = new RouteBuilder(app)
            {
                DefaultHandler = endpoint1,
            };

            routeBuilder.MapRoute("api/status/{item}", c => c.Response.WriteAsync($"{c.GetRouteValue("item")} is just fine."));
            routeBuilder.MapRoute("localized/{lang=en-US}", b =>
            {
                b.Use(next => async (c) =>
                {
                    var culture = new CultureInfo((string)c.GetRouteValue("lang"));
#if DNX451
                    Thread.CurrentThread.CurrentCulture = culture;
                    Thread.CurrentThread.CurrentUICulture = culture;
#else
                    CultureInfo.CurrentCulture = culture;
                    CultureInfo.CurrentUICulture = culture;
#endif
                    await next(c);
                });

                b.Run(c => c.Response.WriteAsync($"What would you do with {1000000m:C}?"));
            });

            routeBuilder.AddPrefixRoute("api/store", endpoint1);
            routeBuilder.AddPrefixRoute("hello/world", endpoint2);

            routeBuilder.MapLocaleRoute("en-US", "store/US/{action}", new { controller = "Store" });
            routeBuilder.MapLocaleRoute("en-GB", "store/UK/{action}", new { controller = "Store" });

            routeBuilder.AddPrefixRoute("", endpoint2);

            app.UseRouter(routeBuilder.Build());
        }