question

AmitRawat-3636 avatar image
0 Votes"
AmitRawat-3636 asked FeiXue-MSFT answered

ASP.NET Core Run Middleware behave differently with different browser.

Below is my middleware code:

app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
ctx.Context.Response.Headers[HeaderNames.CacheControl] =
"public,max-age=10"; // Here I placed my first breakpoint
}
});
app.Run(async context =>
{
await context.Response.WriteAsync("<html><body><a href='images/image1.jpg' target='_blank'>Click for Image1</a></body></html>"); // Here is my Second Breakpoint
});

I placed a break point within Run Middleware as well as with in UseStaticFiles Middleware.
I just cached the static files for 10 seconds.
Then I click on Anchor tag created with in my run middle ware from browser. When I hit this anchor tag from chrome. First time it hit both the break point and rightly so. From next time onward, it only hit Second break point. If it is cached then it should not hit our application. And cache is working that's why UseStaticFiles is not hit.

But when I hit via Firefox. From second request onward it never hit our Run Middleware. Even after cache duration is over. After 10 second it hit UseStaticFiles but not RUN middleware.

What is happening. Any idea?

dotnet-aspnet-core-mvc
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,@AmitRawat-3636,
check response headers in Chrome and FireFox,is there any difference?

0 Votes 0 ·

Hi @YiyiYou-MSFT ,
I only set the cache control property in middleware. It's and empty project. No view no controller. Nothing.
I verified and Response header is same in both the browsers. Also, checked with edge.
Edge is also behaving same as chrome.
Firefox is behaving differently or correct I would say.

----Update
Checked with internet explorer as well. It is working correct like Firefox.

So, Firefox and internet explorer behave similar and chrome and edge behave same.

0 Votes 0 ·

You did not share the entire startup.cs file so we cannot verify where you placed the configuration. I created a test using your code and caching works as expected. I recommend reading the previous link which might change your point of view.

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
         {
             if (env.IsDevelopment())
             {
                 app.UseDeveloperExceptionPage();
             }
    
             app.UseStaticFiles(new StaticFileOptions
             {
                 OnPrepareResponse = ctx =>
                 {
                     ctx.Context.Response.Headers[HeaderNames.CacheControl] = "public,max-age=10";
                 }
             });
    
             app.Run(async context =>
             {
                 await context.Response.WriteAsync("<html><body><a href='images/image1.jpg' target='_blank'>Click for Image1</a></body></html>"); // Here is my Second Breakpoint
             });
         }



0 Votes 0 ·
Show more comments

1 Answer

FeiXue-MSFT avatar image
0 Votes"
FeiXue-MSFT answered

Please check the cache-control headers for the static files to see whether this http header is added to the response expected and cache settings in the browser to see if the cache is disabled. If the browser doesn't follow the http cache headers, it is recommend that submit the issue for that specific browsers.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.