• .net core web 中使用app.UseRouter的几种使用方式


      public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
            {
                loggerFactory.AddConsole();
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseHangfireServer();
                app.UseHangfireDashboard();
    
                //方式一
                //app.Run(async (context) =>
                //{
                //    await context.Response.WriteAsync("Hello World!");
                //});
    
                //方式二
                //var endpoint = new RouteHandler((c) => c.Response.WriteAsync("Hello, I am Routing!"));
                //app.UseRouter(endpoint);
    
                //方式三
                var endpoint = new RouteHandler((c) =>c.Response.WriteAsync($"Hello, I am Routing! your item is {c.GetRouteValue("item")}"));
                var resolver = app.ApplicationServices.GetRequiredService<IInlineConstraintResolver>();
                var runRoute = new Route(endpoint, "{item=home}", resolver);
                app.UseRouter(runRoute);
    
                //方式四
                var runRoute1 = new Route(endpoint, "{item=home}", resolver);
                var otherRoute = new Route(endpoint, "other/{item=other_home}", resolver);
                var routeCollection = new RouteCollection();
                routeCollection.Add(runRoute1);
                routeCollection.Add(otherRoute);
                app.UseRouter(routeCollection);
    
                //方式五
                var routeBuilder = new RouteBuilder(app)
                {
                    DefaultHandler = endpoint,
                };
                routeBuilder.MapRoute("default", "{item=home}");
                routeBuilder.MapRoute("other", "other/{item=other_home}");
    
                app.UseRouter(routeBuilder.Build());
    
                //方式六,改写handler
                var myendpoint = new MyRouteHandler();
                var myrouteBuilder = new RouteBuilder(app)
                {
                    DefaultHandler = myendpoint,
                };
                myrouteBuilder.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
                app.UseRouter(myrouteBuilder.Build());
    
            }

    自定义hanlder

     public class MyRouteHandler : IRouter
        {
            public VirtualPathData GetVirtualPath(VirtualPathContext context)
            {
                return null;
            }
    
            public Task RouteAsync(RouteContext context)
            {
    
                context.Handler = (c) =>
                {
                    var printStr = $"controller:{c.GetRouteValue("controller")}," +
                    $"action:{c.GetRouteValue("action")},id:{c.GetRouteValue("id")}";
                    return c.Response.WriteAsync(printStr);
                };
                return Microsoft.AspNetCore.Routing.Internal.TaskCache.CompletedTask;
            }
        }
  • 相关阅读:
    Android的webview的设置参数
    禁止RecycleView滑动
    Volley加载不出图片的问题
    LIstview滑动时不加载图片,停止时加载!
    【原创】设计模式开篇—面向对象的软件设计
    [原创]写给自己的总结—2014到2015
    【原创】开车分四个阶段的话,你属于哪个
    【原创】亲身经历的几次合同陷阱
    【转】程序员需谨记的8条团队开发原则
    【转】绩效考核的10大误区
  • 原文地址:https://www.cnblogs.com/airven/p/5948774.html
Copyright © 2020-2023  润新知