Search code examples
c#httphandleriis-7.5cache-control

Add web.config static content settings for custom httphandler


I have a custom httphandler that serves static files from a virtual file system. If I configure the static content section like below I would like these settings to apply to the static files i serve via the handler. I guess I need to add the settings to the response my self or is there another way? My handler implements the IHttpHandler interface.

  <location path="Storage">
    <system.webServer>
      <handlers>
        <clear />
        <add name="StaticFile" path="*.jpg" verb="*" type="Stormbreaker.Web.StaticFileHandler, Stormbreaker" />
      </handlers>
      <staticContent>
        <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" />
        <mimeMap fileExtension=".jpg" mimeType="image/jpeg" />
      </staticContent>
    </system.webServer>
  </location>

Solution

  • I believe that staticContent only affects the StaticFileHandler which ships with IIS. IIS have no way of knowing that your handler serves static files, so you would need to add those headers your self in your handler, but you might want to make it configurable from your web.config with it's own section for future changes.