title: ASP.net Core Static Files - How to return a default file (per extension) if the one requested doesn't exist?
body: <p>I understand how to set up serving static files on an ASP.net Core web application, per <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?tabs=aspnetcore2x" rel="nofollow noreferrer">this official guide</a>. I don't want to return a default.html page if the image (for example) they're requesting for doesn't exist.</p>
  
  <p>In <code>Startup.cs</code>'s <code>Configure(IApplicationBuilder app, IHostingEnvironment env);</code>, I have <code>app.UseStaticFiles()</code>.</p>
  
  <p>My <code>Program.cs</code> file:</p>
  
  <pre><code>public class Program
  {
      public static void Main(string[] args)
      {
          BuildWebHost(args).Run();
      }
  
      public static IWebHost BuildWebHost(string[] args) =&gt;
          WebHost.CreateDefaultBuilder(args)
              .UseStartup&lt;Startup&gt;()
              .UseDefaultServiceProvider(options =&gt;
                  options.ValidateScopes = false)
              .Build();
  }
  </code></pre>
  
  <p>Which will, by default, allow me to store to and serve files from that <code>wwwroot</code> folder in my application.</p>
  
  <p>If I have the image file <code>mypic.jpg</code> in <code>wwwroot</code>, I can open my browser and go to <code>http://localhost:53020/mypic.jpg</code> to view it.</p>
  
  <p>Suppose I already deployed this app and someone were to go to their browser and go to, or in their web page, set an <code>img</code>'s <code>src</code> equal to <code>http://mydeployedsite.com/nonexistentpic.jpg</code>, They would get a 404, or a default page, depending on which one I chose. However, I want to serve an image whose graphic indicates "file not found", instead. I don't see anywhere in that guide I linked that gives me an option to serve default files, per extension, if the one requested doesn't exist.</p>
  
  <p>I will probably have to write middleware for what I want. I understand how to write basic middleware, but in such a middleware, I don't know how to intercept a 404 response that came from the static files middleware. Or, there may be more options in the static files middleware not included in that official document. What other approaches can I take besides serving files via routes and controller functions?</p>