diff --git a/samples/StatusCodePagesSample/Startup.cs b/samples/StatusCodePagesSample/Startup.cs index 2c81bb949b..cd2a86812c 100644 --- a/samples/StatusCodePagesSample/Startup.cs +++ b/samples/StatusCodePagesSample/Startup.cs @@ -15,14 +15,14 @@ namespace StatusCodePagesSample public void Configure(IApplicationBuilder app) { app.UseErrorPage(ErrorPageOptions.ShowAll); - app.UseStatusCodePages(new StatusCodePagesOptions() // There is a default response but any of the following can be used to change the behavior. - // .WithHandler(context => context.HttpContext.Response.SendAsync("Handler, status code: " + context.HttpContext.Response.StatusCode, "text/plain")) - // .WithResponse("text/plain", "Response, status code: {0}") - // .WithRedirect("~/errors/{0}") // PathBase relative - // .WithRedirect("/base/errors/{0}") // Absolute - // .WithTangent(builder => builder.UseWelcomePage()) - // .WithReExecute("/errors/{0}") - ); + //app.UseStatusCodePages(); // There is a default response but any of the following can be used to change the behavior. + + // app.WithHandler(context => context.HttpContext.Response.SendAsync("Handler, status code: " + context.HttpContext.Response.StatusCode, "text/plain")); + // app.WithResponse("text/plain", "Response, status code: {0}"); + // app.WithRedirect("~/errors/{0}"); // PathBase relative + // app.WithRedirect("/base/errors/{0}"); // Absolute + // app.WithPipeline(builder => builder.UseWelcomePage()); + // app.WithReExecute("/errors/{0}"); // "/[?statuscode=400]" app.Use((context, next) => diff --git a/src/Microsoft.AspNet.Diagnostics/StatusCodePagesExtensions.cs b/src/Microsoft.AspNet.Diagnostics/StatusCodePagesExtensions.cs index 42c41a353a..c37980bb3e 100644 --- a/src/Microsoft.AspNet.Diagnostics/StatusCodePagesExtensions.cs +++ b/src/Microsoft.AspNet.Diagnostics/StatusCodePagesExtensions.cs @@ -12,39 +12,51 @@ namespace Microsoft.AspNet.Builder public static class StatusCodePagesExtensions { /// - /// Adds a middleware that checks for responses with status codes between 400 and 599 that do not - /// have a body. Several approaches can be used to generate the response body. + /// Adds a StatusCodePages middleware with the given options that checks for responses with status codes + /// between 400 and 599 that do not have a body. /// /// /// /// - public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, StatusCodePagesOptions options) + public static IApplicationBuilder UseStatusCodePages([NotNull]this IApplicationBuilder app, StatusCodePagesOptions options) { return app.UseMiddleware(options); } /// - /// Specifies the handler to invoke to generate the response body. + /// Adds a StatusCodePages middleware with a default response handler that checks for responses with status codes + /// between 400 and 599 that do not have a body. /// - /// - /// + /// /// - public static StatusCodePagesOptions WithHandler(this StatusCodePagesOptions options, Func handler) + public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app) { - options.HandleAsync = handler; - return options; + return UseStatusCodePages(app, new StatusCodePagesOptions()); } /// - /// Specifies the response body to send. This may include a '{0}' placeholder for the status code. + /// Adds a StatusCodePages middleware with the specified handler that checks for responses with status codes + /// between 400 and 599 that do not have a body. /// - /// + /// + /// + /// + public static IApplicationBuilder WithHandler(this IApplicationBuilder app, Func handler) + { + return UseStatusCodePages(app, new StatusCodePagesOptions() { HandleAsync = handler }); + } + + /// + /// Adds a StatusCodePages middleware with the specified response body to send. This may include a '{0}' placeholder for the status code. + /// The middleware checks for responses with status codes between 400 and 599 that do not have a body. + /// + /// /// /// /// - public static StatusCodePagesOptions WithResponse(this StatusCodePagesOptions options, string contentType, string bodyFormat) + public static IApplicationBuilder WithResponse(this IApplicationBuilder app, string contentType, string bodyFormat) { - return options.WithHandler(context => + return WithHandler(app, context => { var body = string.Format(CultureInfo.InvariantCulture, bodyFormat, context.HttpContext.Response.StatusCode); return context.HttpContext.Response.SendAsync(body, contentType); @@ -52,19 +64,19 @@ namespace Microsoft.AspNet.Builder } /// - /// Specifies that responses should be handled by redirecting with the given location URL template. - /// This may include a '{0}' placeholder for the status code. URLs starting with '~' will have PathBase prepended, - /// where any other URL will be used as is. + /// Adds a StatusCodePages middleware to the pipeine. Specifies that responses should be handled by redirecting + /// with the given location URL template. This may include a '{0}' placeholder for the status code. URLs starting + /// with '~' will have PathBase prepended, where any other URL will be used as is. /// - /// + /// /// /// - public static StatusCodePagesOptions WithRedirect(this StatusCodePagesOptions options, string locationFormat) + public static IApplicationBuilder WithRedirect(this IApplicationBuilder app, string locationFormat) { if (locationFormat.StartsWith("~")) { locationFormat = locationFormat.Substring(1); - return options.WithHandler(context => + return WithHandler(app, context => { var location = string.Format(CultureInfo.InvariantCulture, locationFormat, context.HttpContext.Response.StatusCode); context.HttpContext.Response.Redirect(context.HttpContext.Request.PathBase + location); @@ -73,7 +85,7 @@ namespace Microsoft.AspNet.Builder } else { - return options.WithHandler(context => + return WithHandler(app, context => { var location = string.Format(CultureInfo.InvariantCulture, locationFormat, context.HttpContext.Response.StatusCode); context.HttpContext.Response.Redirect(location); @@ -83,29 +95,30 @@ namespace Microsoft.AspNet.Builder } /// - /// Specifies an alternate middleware pipeline to execute to generate the response body. + /// Adds a StatusCodePages middleware to the pipeline with the specified alternate middleware pipeline to execute + /// to generate the response body. /// - /// + /// /// /// - public static StatusCodePagesOptions WithTangent(this StatusCodePagesOptions options, Action configuration) + public static IApplicationBuilder WithPipeline(this IApplicationBuilder app, Action configuration) { - var builder = new ApplicationBuilder(serviceProvider: null); // TODO: services + var builder = app.New(); configuration(builder); var tangent = builder.Build(); - return options.WithHandler(context => tangent(context.HttpContext)); + return WithHandler(app, context => tangent(context.HttpContext)); } /// - /// Specifies that the response body should be generated by re-executing the request pipeline using an alternate path. - /// This path may contain a '{0}' placeholder of the status code. + /// Adds a StatusCodePages middleware to the pipeline. Specifies that the response body should be generated by + /// re-executing the request pipeline using an alternate path. This path may contain a '{0}' placeholder of the status code. /// - /// + /// /// /// - public static StatusCodePagesOptions WithReExecute(this StatusCodePagesOptions options, string pathFormat) + public static IApplicationBuilder WithReExecute(this IApplicationBuilder app, string pathFormat) { - return options.WithHandler(async context => + return WithHandler(app, async context => { var newPath = new PathString(string.Format(CultureInfo.InvariantCulture, pathFormat, context.HttpContext.Response.StatusCode)); diff --git a/src/Microsoft.AspNet.Diagnostics/StatusCodePagesMiddleware.cs b/src/Microsoft.AspNet.Diagnostics/StatusCodePagesMiddleware.cs index 32b95c470a..a123a367bf 100644 --- a/src/Microsoft.AspNet.Diagnostics/StatusCodePagesMiddleware.cs +++ b/src/Microsoft.AspNet.Diagnostics/StatusCodePagesMiddleware.cs @@ -13,13 +13,13 @@ namespace Microsoft.AspNet.Diagnostics private readonly RequestDelegate _next; private readonly StatusCodePagesOptions _options; - public StatusCodePagesMiddleware(RequestDelegate next, StatusCodePagesOptions options) + public StatusCodePagesMiddleware(RequestDelegate next, [NotNull]StatusCodePagesOptions options) { _next = next; _options = options; if (_options.HandleAsync == null) { - throw new ArgumentException("Missing HandleAsync implementation."); + throw new ArgumentException("Missing options.HandleAsync implementation."); } }