I have this middleware:
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;
}
}
Quite simply, when I get something like this through: subdomain.mydomain.comI want to show a specific html page, otherwise transferring a regular middleware pipeline to www.mydomain.com.
When this middleware hits, it ends as 404 in the browser. If I do not set the content type, it ends as 200, and the entire html is written out as text, and then displayed as html. What am I missing here?
I do not want to use app.UseDefaultFiles()or app.UseStaticFiles().
source
share