.net 核心中的中间件无法正常工作,让我介入其中

Middleware in .net core not working, letting me step into it

我意识到我需要为 httpcontext 等编写一些中间件,但因此我试图从 Microsoft 举个例子,问题是即使在外部有断点... app.Useapp.Run ,使用 F11 它不会进入代码。

我怎样才能进入这段代码来查看这些值?

startup.cs 文件

public void Configure(IApplicationBuilder app)
{

    var request = new Request("api/menu/create", Method.POST);
    request.AddParameter("currentApplicationId", 1, ParameterType.QueryString);


    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();
    // UPDATE :  Code above prevented from being able to step into below?  

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });

    app.Use((context, next) =>
    {
        var cultureQuery = context.Request.Query["culture"];
        if (!string.IsNullOrWhiteSpace(cultureQuery))
        {
            var culture = new CultureInfo(cultureQuery);
            CultureInfo.CurrentCulture = culture;
            CultureInfo.CurrentUICulture = culture;
        }

        // Call the next delegate/middleware in the pipeline
        return next();
    });

    app.Run(async (context) =>
    {
        await context.Response.WriteAsync(
        $"Hello {CultureInfo.CurrentCulture.DisplayName}");
    });

}

在ASP.NET核心请求处理管道app.UseMvc()应该是最后一个Middleware如下,否则下一个中间件不会调用。

app.Use((context, next) =>
{
    var cultureQuery = context.Request.Query["culture"];
    if (!string.IsNullOrWhiteSpace(cultureQuery))
    {
        var culture = new CultureInfo(cultureQuery);
        CultureInfo.CurrentCulture = culture;
        CultureInfo.CurrentUICulture = culture;
    }

    // Call the next delegate/middleware in the pipeline
    return next();
});

app.Run(async (context) =>
{
    await context.Response.WriteAsync(
    $"Hello {CultureInfo.CurrentCulture.DisplayName}");
});

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

希望能解决您的问题。

请在中间或开头使用添加中间件

//Add our new middleware to the pipeline
app.UseMiddleware();

app.UseSwagger();