Skip to content

Dotnet 6 ommits If-None-Match Request header making ETag usage impossible #39516

@aviita

Description

@aviita

Description

Trying to implement ETag for an index.html serving react JS bundles. Implemented a SHA1 based ETag hash for the configs.

Reproduction Steps

  1. Create DotNet 6 WebApp with "ASP.NET Core Web App (Model-View-Controller)" template
  2. Put HomeController.cs, Program.cs and appsettings.json from below in place
  3. Put Breakpoint into HomeController.Index() line 24: return new StatusCodeResult(StatusCodes.Status304NotModified);
  4. Run application
  5. Refresh page

Expected behavior

Breakpoint is hit or not modified response is returned.

Actual behavior

  • Breakpoint is newer hit.
  • Header is not to be found from the request headers (when debugging the Index method)
  • Browser does seem to send the header (see below image)
    image

Regression?

No response

Known Workarounds

No response

Configuration

DotNet 6.0.101
VS 2022

Microsoft Edge 97.0.1072.55 (Official build) (64-bit)
Revision ce3bf3364880558d4945082f4ac36e01fe36716e
Operating system Windows 10 Version 21H1 (Build 19043.1466)

Other information

Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    //app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.Use(async (context, next) =>
{
    context.Response.GetTypedHeaders().CacheControl =
        new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
        {
            Public = true,
            //MaxAge = TimeSpan.FromMinutes(1),
            MustRevalidate = true,
        };
    context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Vary] =
        new string[] { "Accept-Encoding" };

    await next();
});

//app.UseResponseCaching();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

HomeController.cs:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Net.Http.Headers;
using MvcWebApplication1.Models;
using System.Diagnostics;

namespace MvcWebApplication1.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IConfiguration _configuration;

        public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            var currentETag = _configuration["EtagHashValue"] ?? "";
            var reqETag = Request.Headers["if-none-match"];
            if (reqETag == currentETag)
                return new StatusCodeResult(StatusCodes.Status304NotModified);

            Response.Headers.Add(HeaderNames.ETag, new[] { currentETag });

            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

appsettings.js:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "EtagHashValue": "abdcd",
  "AllowedHosts": "*"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.Status: No Recent Activityarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions