Cal*_*ton 6 c# dnx asp.net-core
我有这个中间件:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
很简单,当我得到这样的东西时:subdomain.mydomain.com我想显示一个特定的html页面,否则进行正常的中间件管道www.mydomain.com.
当这个中间件被击中时,它最终在浏览器中成为404.如果我没有设置内容类型,那么它最终为200,所有html都写为文本,而不是呈现为html.我在这里错过了什么?
我不想用app.UseDefaultFiles()或app.UseStaticFiles().
你犯的一个错误是:
await this.ReturnIndexPage(context); // wrong!
await SpecificPageMiddleware.ReturnIndexPage(context); // right (1)
await ReturnIndexPage(context); // right (2)
Run Code Online (Sandbox Code Playgroud)
this意味着实例.您无法static从实例访问方法.相反,你必须用类型名称(1)或没有资格(2)来限定它,你没事.
为了更好的衡量,这也是GitHub的演示.
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
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!");
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果
| 归档时间: |
|
| 查看次数: |
2965 次 |
| 最近记录: |