user10189172
user10189172

Reputation:

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

I was realizing that I need to write some middleware for httpcontext etc.., but thus I tried to take even an example from Microsoft, and the problem is that even with breakpoints on the outside ... app.Use and app.Run , with F11 it will not step into the code.

How can I even step into this code to see the values?

startup.cs file

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}");
    });

}

Upvotes: 6

Views: 9012

Answers (2)

Renuka
Renuka

Reputation: 9

Please use adding middleware in the mid or beginning

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

app.UseSwagger();

Upvotes: 0

TanvirArjel
TanvirArjel

Reputation: 32059

In ASP.NET Core request processing pipeline app.UseMvc() should be the last Middleware as follows, otherwise next middlewares would not call.

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?}");
});

Hope it will solve your problem.

Upvotes: 6

Related Questions