Changing extensions from StatusCodePagesOptions to IApplicationBuilder.

Addressing few more PR comments.
This commit is contained in:
Praburaj 2015-02-10 16:18:06 -08:00
parent 6850035d8e
commit 23ef6d4eb8
3 changed files with 53 additions and 40 deletions

View File

@ -15,14 +15,14 @@ namespace StatusCodePagesSample
public void Configure(IApplicationBuilder app) public void Configure(IApplicationBuilder app)
{ {
app.UseErrorPage(ErrorPageOptions.ShowAll); 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. //app.UseStatusCodePages(); // 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}") // app.WithHandler(context => context.HttpContext.Response.SendAsync("Handler, status code: " + context.HttpContext.Response.StatusCode, "text/plain"));
// .WithRedirect("~/errors/{0}") // PathBase relative // app.WithResponse("text/plain", "Response, status code: {0}");
// .WithRedirect("/base/errors/{0}") // Absolute // app.WithRedirect("~/errors/{0}"); // PathBase relative
// .WithTangent(builder => builder.UseWelcomePage()) // app.WithRedirect("/base/errors/{0}"); // Absolute
// .WithReExecute("/errors/{0}") // app.WithPipeline(builder => builder.UseWelcomePage());
); // app.WithReExecute("/errors/{0}");
// "/[?statuscode=400]" // "/[?statuscode=400]"
app.Use((context, next) => app.Use((context, next) =>

View File

@ -12,39 +12,51 @@ namespace Microsoft.AspNet.Builder
public static class StatusCodePagesExtensions public static class StatusCodePagesExtensions
{ {
/// <summary> /// <summary>
/// Adds a middleware that checks for responses with status codes between 400 and 599 that do not /// Adds a StatusCodePages middleware with the given options that checks for responses with status codes
/// have a body. Several approaches can be used to generate the response body. /// between 400 and 599 that do not have a body.
/// </summary> /// </summary>
/// <param name="app"></param> /// <param name="app"></param>
/// <param name="options"></param> /// <param name="options"></param>
/// <returns></returns> /// <returns></returns>
public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app, StatusCodePagesOptions options) public static IApplicationBuilder UseStatusCodePages([NotNull]this IApplicationBuilder app, StatusCodePagesOptions options)
{ {
return app.UseMiddleware<StatusCodePagesMiddleware>(options); return app.UseMiddleware<StatusCodePagesMiddleware>(options);
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="options"></param> /// <param name="app"></param>
/// <param name="handler"></param>
/// <returns></returns> /// <returns></returns>
public static StatusCodePagesOptions WithHandler(this StatusCodePagesOptions options, Func<StatusCodeContext, Task> handler) public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder app)
{ {
options.HandleAsync = handler; return UseStatusCodePages(app, new StatusCodePagesOptions());
return options;
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="options"></param> /// <param name="app"></param>
/// <param name="handler"></param>
/// <returns></returns>
public static IApplicationBuilder WithHandler(this IApplicationBuilder app, Func<StatusCodeContext, Task> handler)
{
return UseStatusCodePages(app, new StatusCodePagesOptions() { HandleAsync = handler });
}
/// <summary>
/// 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.
/// </summary>
/// <param name="app"></param>
/// <param name="contentType"></param> /// <param name="contentType"></param>
/// <param name="bodyFormat"></param> /// <param name="bodyFormat"></param>
/// <returns></returns> /// <returns></returns>
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); var body = string.Format(CultureInfo.InvariantCulture, bodyFormat, context.HttpContext.Response.StatusCode);
return context.HttpContext.Response.SendAsync(body, contentType); return context.HttpContext.Response.SendAsync(body, contentType);
@ -52,19 +64,19 @@ namespace Microsoft.AspNet.Builder
} }
/// <summary> /// <summary>
/// Specifies that responses should be handled by redirecting with the given location URL template. /// Adds a StatusCodePages middleware to the pipeine. Specifies that responses should be handled by redirecting
/// This may include a '{0}' placeholder for the status code. URLs starting with '~' will have PathBase prepended, /// with the given location URL template. This may include a '{0}' placeholder for the status code. URLs starting
/// where any other URL will be used as is. /// with '~' will have PathBase prepended, where any other URL will be used as is.
/// </summary> /// </summary>
/// <param name="options"></param> /// <param name="app"></param>
/// <param name="locationFormat"></param> /// <param name="locationFormat"></param>
/// <returns></returns> /// <returns></returns>
public static StatusCodePagesOptions WithRedirect(this StatusCodePagesOptions options, string locationFormat) public static IApplicationBuilder WithRedirect(this IApplicationBuilder app, string locationFormat)
{ {
if (locationFormat.StartsWith("~")) if (locationFormat.StartsWith("~"))
{ {
locationFormat = locationFormat.Substring(1); locationFormat = locationFormat.Substring(1);
return options.WithHandler(context => return WithHandler(app, context =>
{ {
var location = string.Format(CultureInfo.InvariantCulture, locationFormat, context.HttpContext.Response.StatusCode); var location = string.Format(CultureInfo.InvariantCulture, locationFormat, context.HttpContext.Response.StatusCode);
context.HttpContext.Response.Redirect(context.HttpContext.Request.PathBase + location); context.HttpContext.Response.Redirect(context.HttpContext.Request.PathBase + location);
@ -73,7 +85,7 @@ namespace Microsoft.AspNet.Builder
} }
else else
{ {
return options.WithHandler(context => return WithHandler(app, context =>
{ {
var location = string.Format(CultureInfo.InvariantCulture, locationFormat, context.HttpContext.Response.StatusCode); var location = string.Format(CultureInfo.InvariantCulture, locationFormat, context.HttpContext.Response.StatusCode);
context.HttpContext.Response.Redirect(location); context.HttpContext.Response.Redirect(location);
@ -83,29 +95,30 @@ namespace Microsoft.AspNet.Builder
} }
/// <summary> /// <summary>
/// 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.
/// </summary> /// </summary>
/// <param name="options"></param> /// <param name="app"></param>
/// <param name="configuration"></param> /// <param name="configuration"></param>
/// <returns></returns> /// <returns></returns>
public static StatusCodePagesOptions WithTangent(this StatusCodePagesOptions options, Action<IApplicationBuilder> configuration) public static IApplicationBuilder WithPipeline(this IApplicationBuilder app, Action<IApplicationBuilder> configuration)
{ {
var builder = new ApplicationBuilder(serviceProvider: null); // TODO: services var builder = app.New();
configuration(builder); configuration(builder);
var tangent = builder.Build(); var tangent = builder.Build();
return options.WithHandler(context => tangent(context.HttpContext)); return WithHandler(app, context => tangent(context.HttpContext));
} }
/// <summary> /// <summary>
/// Specifies that the response body should be generated by re-executing the request pipeline using an alternate path. /// Adds a StatusCodePages middleware to the pipeline. Specifies that the response body should be generated by
/// This path may contain a '{0}' placeholder of the status code. /// re-executing the request pipeline using an alternate path. This path may contain a '{0}' placeholder of the status code.
/// </summary> /// </summary>
/// <param name="options"></param> /// <param name="app"></param>
/// <param name="pathFormat"></param> /// <param name="pathFormat"></param>
/// <returns></returns> /// <returns></returns>
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)); var newPath = new PathString(string.Format(CultureInfo.InvariantCulture, pathFormat, context.HttpContext.Response.StatusCode));

View File

@ -13,13 +13,13 @@ namespace Microsoft.AspNet.Diagnostics
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly StatusCodePagesOptions _options; private readonly StatusCodePagesOptions _options;
public StatusCodePagesMiddleware(RequestDelegate next, StatusCodePagesOptions options) public StatusCodePagesMiddleware(RequestDelegate next, [NotNull]StatusCodePagesOptions options)
{ {
_next = next; _next = next;
_options = options; _options = options;
if (_options.HandleAsync == null) if (_options.HandleAsync == null)
{ {
throw new ArgumentException("Missing HandleAsync implementation."); throw new ArgumentException("Missing options.HandleAsync implementation.");
} }
} }