Skip to content

MapStaticFiles suppress UseStaticFile #58942

@andrew-vdb

Description

@andrew-vdb

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

app.UseStaticFiles(new StaticFileOptions()
{
    OnPrepareResponse = ctx =>
    {
        if (ctx.Context.Request.Path.Value.Contains("/ui/"))//Do not use StartWithSegments here!
        {
            if (!ctx.Context.User.Identity.IsAuthenticated)
            {
                ctx.Context.Abort();
                ctx.Context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
                return;
            }
        }
    },
})
app.MapStaticAssets();

When using MapStaticAssets after UseStaticFiles, the code inside UseStaticFiles (OnPrepareResponse) is not get called

Expected Behavior

The goal is to use both features
For certain static files, use logic in UseStaticFiles, otherwise use MapStaticAssets

Steps To Reproduce

Put breakpoint inside OnPrepareResponse, it won't be hit if there is app.MapStaticAssets

Exceptions (if any)

No response

.NET Version

9.0.0

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions