ASP NET Core服务特定的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()

肖恩·卢汀(Shaun Luttin)

回答。

您在这里犯的一个错误是:

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

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

启动文件

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

结果

简单查询字符串

没有查询字串

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

防止特定的asp.net页面被注销

来自分类Dev

ASP.NET Core中的服务总线

来自分类Dev

带有ASP.NET Core的“联系我们”页面-SMTP服务器问题

来自分类Dev

带有剃刀页面的 ASP.NET Core 中的公共服务

来自分类Dev

ASP.NET-Core嵌套的ASP TagHelper

来自分类Dev

ASP.NET Core返回

来自分类Dev

ASP.NET Core WebSockets

来自分类Dev

ASP.NET Core Web Api自动帮助页面

来自分类Dev

Asp .Net Core 3.1 Razor页面:复杂模型绑定

来自分类Dev

在ASP.NET Core MVC中设置新的登录页面

来自分类Dev

Asp.net Core Razor页面中的自举警报

来自分类Dev

简单的ASP.Net Core Razor页面上的HTTP 400

来自分类Dev

Asp.NET Core MVC Razor页面渲染中的NotSupportedException

来自分类Dev

在ASP.net Core 3.1 Razor页面中打印PDF

来自分类Dev

部分剃刀页面无法在ASP .NET Core中呈现

来自分类Dev

显示ASP.NET Core MVC的404找不到页面

来自分类Dev

ASP.NET Core 2 razor 页面中的文件上传

来自分类Dev

ASP.NET Core Razor 页面 - 不绑定 POST 请求

来自分类Dev

在特定页面或位置ASP.NET合并PDF文件

来自分类Dev

如何使用Python搜寻特定的ASP.NET页面?

来自分类Dev

与Asp.net相比,ASP.net CORE的主要优势

来自分类Dev

与ASP.NET MVC BeginExecuteCore等效的ASP.NET Core

来自分类Dev

从asp net 5迁移到asp net core时出错

来自分类Dev

asp .net 和 asp .net core 有什么区别?

来自分类Dev

ASP.NET Core作为Windows服务wwwroot的位置

来自分类Dev

ASP.NET Core服务.ConfigureServices启动方法中的AddScoped

来自分类Dev

ASP.NET Core Expose Config到DI注入服务

来自分类Dev

ASP.NET Core中的OAuth授权服务

来自分类Dev

Azure函数与ASP.NET Core工作者服务?