title: ASP NET Core Serving specific html page
body: <p>I have this middleware:</p>
  
  <pre><code>public class SpecificPageMiddleware
  {
      private readonly RequestDelegate next;
  
      public SpecificPageMiddleware(RequestDelegate next)
      {
          this.next = next;
      }
  
      public async Task Invoke(HttpContext context)
      {
          if (this.IsSubDomainRequest(context.Request.Host.Value)) 
          {
              if (this.IsIndexRequest(context.Request.Path.Value)) 
              {
                  await this.ReturnIndexPage(context);
                  return;
              }
          }
  
          await this.next.Invoke(context);
      }
  
      private bool IsSubDomainRequest(string host)
      {
          return host.StartsWith("subdomain")
                  || host.Contains("subdomain");
      }
  
      private bool IsIndexRequest(string query)
      {
          return query == "/" || query == "/response.html";
      }
  
      private static async Task ReturnIndexPage(HttpContext context)
      {
          var file = new FileInfo(@"wwwroot\response.html");
          byte[] buffer;
          if (file.Exists)
          {
              context.Response.StatusCode = (int)HttpStatusCode.OK;
              context.Response.ContentType = "text/html";
  
              buffer = File.ReadAllBytes(file.FullName);
          }
          else
          {
              context.Response.StatusCode = (int)HttpStatusCode.NotFound;
              context.Response.ContentType = "text/plain";
  
              buffer = Encoding.UTF8.GetBytes("Unable to find the requested file");
          }
  
          using (var stream = context.Response.Body)
          {
              await stream.WriteAsync(buffer, 0, buffer.Length);
              await stream.FlushAsync();
          }
  
          context.Response.ContentLength = buffer.Length;
      }
  }
  </code></pre>
  
  <p>Quite simply, when I get something like this through: <code>subdomain.mydomain.com</code> I want to show a specific html page otherwise carry on the normal middleware pipeline to <code>www.mydomain.com</code>.</p>
  
  <p>When this middleware gets hit, it ends up as a 404 in the browser. If I don't set a content type then it ends up as 200 with all the html written out as text, rather then rendered as html. What am I missing here?</p>
  
  <p>I don't want to use <code>app.UseDefaultFiles()</code> or <code>app.UseStaticFiles()</code>.</p>