MVC6中的自定义选择性404页面

时间:2016-01-18 13:15:30

标签: asp.net-core asp.net-core-mvc

我正在寻求帮助,我们可以在ASP.NET 5 MVC6中拥有自定义的Error404页面。

我最接近的是使用

app.UseStatusCodePagesWithReExecute("/Error/{0}");

在Startup.cs配置方法中。

通过调用" / Error / 404"它可以正常工作行动。

如果缺少JPG / PNG / GIF请求,我正在研究如何发送不同的404响应。

任何正确方向的建议都会有很大帮助。

3 个答案:

答案 0 :(得分:3)

如果您使用当前的UseStatusCodePages中间件,它将影响每个错误。为了实现您正在寻找的东西,您需要创建自己的中间件,需要在您的默认错误处理中间件之后立即放置:

//Startup.cs
public void Configure(IApplicationBuilder app)
{
    app.UseStatusCodePagesWithReExecute("/Home/Error/{0}");
    app.UseImageNotFoundMiddlewareWithRedirect("/Home/ImageError");
}

这应该让你开始:Github - StatusCodePage

这是一个示例中间件,可以模拟您正在寻找的内容:

public class ImageNotFoundMiddleware
{
    private readonly RequestDelegate _next;
    private readonly StatusCodePagesOptions _options;

    public ImageNotFoundMiddleware(RequestDelegate next, IOptions<StatusCodePagesOptions> options)
    {
        _next = next;
        _options = options.Value;
        if (_options.HandleAsync == null)
        {
            throw new ArgumentException("Missing options.HandleAsync implementation.");
        }
    }

    public async Task Invoke(HttpContext context)
    {
        var statusCodeFeature = new StatusCodePagesFeature();
        context.Features.Set<IStatusCodePagesFeature>(statusCodeFeature);

        await _next(context);

        if (!statusCodeFeature.Enabled)
        {
            // Check if the feature is still available because other middleware (such as a web API written in MVC) could
            // have disabled the feature to prevent HTML status code responses from showing up to an API client.
            return;
        }

        // Do nothing if a response body has already been provided or not 404 response
        if (context.Response.HasStarted
            || context.Response.StatusCode != 404
            || context.Response.ContentLength.HasValue
            || !string.IsNullOrEmpty(context.Response.ContentType))
        {
            return;
        }

        // todo => Here is where you'd also add your logic to check for the image 404...
        if (context.Request.Path.Value.EndsWith(".JPG", StringComparison.OrdinalIgnoreCase)
            || context.Request.Path.Value.EndsWith(".PNG", StringComparison.OrdinalIgnoreCase)
            || context.Request.Path.Value.EndsWith(".GIF", StringComparison.OrdinalIgnoreCase)
            )
        {
            var statusCodeContext = new StatusCodeContext(context, _options, _next);
            await _options.HandleAsync(statusCodeContext);
        }
    }
}

// Extension method used to add the middleware to the HTTP request pipeline.
public static class ImageNotFoundMiddlewareExtensions
{
    public static IApplicationBuilder UseImageNotFoundMiddlewareWithRedirect(this IApplicationBuilder app,
        string locationFormat)
    {
        if (app == null)
        {
            throw new ArgumentNullException(nameof(app));
        }

        return app.UseMiddleware<ImageNotFoundMiddleware>(
            Options.Create(
                new StatusCodePagesOptions
                {
                    HandleAsync = context =>
                    {
                        var location = string.Format(
                            CultureInfo.InvariantCulture,
                            locationFormat.StartsWith("~") ? locationFormat.Substring(1) : locationFormat,
                            context.HttpContext.Response.StatusCode);
                        context.HttpContext.Response.Redirect(
                            locationFormat.StartsWith("~")
                                ? context.HttpContext.Request.PathBase + location
                                : location);
                        return Task.FromResult(0);
                    }
                }
            )
        );
    }
}

答案 1 :(得分:0)

您应该能够使用UseStatusCodePages()方法的一个覆盖来实现此目的。使用其中一个:

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        // app.UseErrorPage(ErrorPageOptions.ShowAll);
        // app.UseStatusCodePages();
        // app.UseStatusCodePages(context => context.HttpContext.Response.SendAsync("Handler, status code: " + context.HttpContext.Response.StatusCode, "text/plain"));
        // app.UseStatusCodePages("text/plain", "Response, status code: {0}");
        // app.UseStatusCodePagesWithRedirects("~/errors/{0}");
        // app.UseStatusCodePagesWithRedirects("/base/errors/{0}");
        // app.UseStatusCodePages(builder => builder.UseWelcomePage());
        // app.UseStatusCodePagesWithReExecute("/errors/{0}");
    }
} 

答案 2 :(得分:0)

作为自定义中间件方法的替代方法,您可以使用MapWhen分割管道并分别处理图像:

将以下内容添加到Startup.cs的Configure方法(非图像中间件上方):

app.MapWhen(context => context.Request.Path.Value.EndsWith(".png"), appBuilder =>
{
    appBuilder.UseStatusCodePagesWithReExecute("/image-error");
    appBuilder.UseStaticFiles();
});

显然,您可以更改谓词以符合您的需求。

然后您可以创建一个操作和视图来处理错误:

[Route("image-error")]
public IActionResult ImageError(int code)
{
    return View();
}

您可以在我撰写的关于conditional middleware

的帖子中找到更多信息