2016-02-19 32 views
4

我有這樣的中間件:ASP NET核心服務於特定的HTML頁面

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; 
    } 
} 

很簡單,當我得到這樣的事情經過:subdomain.mydomain.com我想顯示特定HTML頁面上正常的中間件管道,否則攜帶www.mydomain.com

當這個中間件被擊中時,它在瀏覽器中結束爲404。如果我沒有設置內容類型,那麼它會以200作爲結果,並將所有html寫成文本,而不是呈現爲html。我在這裏錯過了什麼?

我不想用app.UseDefaultFiles()app.UseStaticFiles()

+0

你嘗試使用Fiddler2記錄的流量?也許你有一些無效的內容之前提交標題 – Tseng

+0

我會看看,我確實看過鉻網絡請求.... –

回答

7

答案。你讓

一個錯誤是在這裏:

await this.ReturnIndexPage(context);      // wrong! 
await SpecificPageMiddleware.ReturnIndexPage(context); // right (1) 
await ReturnIndexPage(context);       // right (2) 

this意味着實例。您不能從實例訪問static方法。相反,您必須使用類型名稱(1)或沒有限定條件(2)來限定它,並且您沒有問題。

作品在我的機器

良好的措施上,this is up on GitHub too as a demo.

SimpleMiddleware.cs

using Microsoft.AspNet.Builder; 
using System.Threading.Tasks; 
using Microsoft.AspNet.Http; 
using System.IO; 
using System.Text; 
using System.Net; 

namespace App04SimpleMiddleware 
{ 
    public class SimpleMiddleware 
    { 
     private readonly RequestDelegate _next; 
     public SimpleMiddleware(RequestDelegate next) 
     { 
      _next = next; 
     } 

     public async Task Invoke(HttpContext context) 
     { 
      if (context.Request.QueryString.ToString().Contains("simple")) 
      { 
       await ReturnIndexPage(context);   // right! 
       return; 
      } 
      await _next.Invoke(context); 
     } 

     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"); 
      } 

      context.Response.ContentLength = buffer.Length; 

      using (var stream = context.Response.Body) 
      { 
       await stream.WriteAsync(buffer, 0, buffer.Length); 
       await stream.FlushAsync(); 
      }  
     } 
    } 
} 

Startup.cs

using Microsoft.AspNet.Builder; 
using Microsoft.AspNet.Http; 

namespace App04SimpleMiddleware 
{ 
    public class Startup 
    { 
     public void Configure(IApplicationBuilder app) 
     { 
      app.UseMiddleware<SimpleMiddleware>();    
      app.Run(async (context) => 
      { 
       await context.Response.WriteAsync("Hello world!"); 
      }); 
     } 
    } 
} 

結果

Simple query string

No query string

+0

你是說我所做的工作在你的機器上? –

+0

不,我是說我改變的版本。更多即將推出。 :) –

+0

哦,好的,謝謝 –