首页 新闻 赞助 找找看

dotnet core 3.1的map会导致context.Request.Path为空

0
悬赏园豆:20 [待解决问题]

Startup.cs代码

using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace middleware
{
    public class Startup
    {
        /// <summary>
        /// 注册应用程序所需的服务
        /// </summary>
        public void ConfigureServices(IServiceCollection service)
        {
            
        }

        /// <summary>
        /// 注册管道中间件
        /// </summary>
        public void Configure(IApplicationBuilder app, IHostEnvironment env)
        {
            // 开发环境,添加开发者异常页面
            if(env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // Use 方式
            app.Use(async (context, next) =>
            {
                if(context.Request.Path == new PathString("/use"))
                {
                    await context.Response.WriteAsync($"Path: {context.Request.Path}");
                }
                await next();
            });

            // UseWhen 方式
            app.UseWhen(context => context.Request.Path == new PathString("/usewhen"),
            a => a.Use(async (context, next) =>
            {
                await context.Response.WriteAsync($"Path: {context.Request.Path}");
                await next();
            }));

            // Map 方式
            app.Map(new PathString("/map"),
            a => a.Use(async (context, next) =>
            {
                Console.WriteLine($"Path: {context.Request.Path}");
                await context.Response.WriteAsync($"Path: {context.Request.Path}");
                foreach(var item in context.Request.Headers)
                {
                    await context.Response.WriteAsync($"\n{item.Key}: {item.Value}");
                }
                await next();
            }));

            // MapWhen 方式
            app.MapWhen(context => context.Request.Path == new PathString("/mapwhen"),
            a => a.Use(async (context, next) =>
            {
                await context.Response.WriteAsync($"Path: {context.Request.Path}");
                await next();
            }));

            // Run 放在最后,可有可无,主要为了验证是否可以回到原来的管道上继续执行
            app.Run(async (context)=>
            {
                await context.Response.WriteAsync("\nCongratulation, return to the original pipe.");
            });
        }
    }
}

访问/map,发现显示的Path为空:

哪位.net大佬帮忙看看?

cxccc的主页 cxccc | 初学一级 | 园豆:162
提问于:2020-06-16 19:16
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册