Search code examples
asp.net.netblazorblazor-webassembly

What is the default ASP.NET http/https launch profiles (launchSettings.json) server and how do I configure it?


For the last few years, the default launch profile for ASP.NET Core projects are http or https (beside IIS Express). I can't find any info on what exactly is launched (is it Kestrel?) or how do I configure it.

For example, I would like to add Cache-Control: no-store to all responses while developing the app. For ASP.NET Core websites, I could add a middleware to handle it but for Blazor WebAssembly apps there is no way to control the response beside IIS Express's web.config (which also is not very convenient to edit).

Please give me a documentation on these configurations. Can I add a response header to the local launchSettings.json file?


According to my search, apparently the only solution currently is to run an additional ASP.NET hosted project.

You can work around this today by running as an ASP.NET Core hosted app and setting the headers in the server project.


Solution

  • Yes it is Kestrel. But you still cannot configure Kestrel to use custom headers.

    Even in the Server project, if you want to add custom header, you will need to use middleware (app pipeline). This has nothing to do with Kestrel configuration. Of course you will be available configure Kestrel using builder.WebHost.ConfigureKestrel()

    In WASM standalone project, Visual studio start Kestrel using Microsoft.AspNetCore.Components.WebAssembly.DevServer. You could also find this reference in csproj (If you delete this reference and rebuild, the debug won't start). And when you check the F12 in browser, there will show Kestrel in response headers:
    enter image description here

    You could configure this Kestrel dev server by createing appsettings in project root (not wwwroot): enter image description here
    Then configure it as if it is a server project: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-8.0#configure-sni-in-appsettingsjson.

    So after all, it is possible to configure Kestrel, but cannot add headers. The only way is using iisExpress $(solutionDir)\.vs\config\applicationhost.config.

     <system.webServer>
        <httpProtocol>
          <customHeaders>
            <add name="Cache-Control" value="no-store" />
          </customHeaders>
        </httpProtocol>
    </system.webServer>