/// <summary> /// Use the status code handlers middleware in an OWIN pipeline. /// </summary> /// <param name="configureOptions">A delegate to configure the middleware options.</param> /// <returns>An OWIN middleware function.</returns> public static MidFunc UseStatusCodeHandlers(Action <StatusCodeHandlersOptions> configureOptions) { var options = new StatusCodeHandlersOptions(); configureOptions(options); return(UseStatusCodeHandlers(options)); }
/// <summary> /// Use the status code handlers middleware in an OWIN pipeline. /// </summary> /// <param name="options">The middleware options.</param> /// <returns>An OWIN middleware function.</returns> public static MidFunc UseStatusCodeHandlers(StatusCodeHandlersOptions options) { return (next => async env => { var responseBody = env.Get <Stream>(ResponseBodyKey); var streamWrapper = new StreamWrapper(responseBody); env[ResponseBodyKey] = streamWrapper; await next(env); if (!streamWrapper.WriteOccured) { var statusCode = env.Get <int?>(ResponseStatusCodeKey) ?? 200; AppFunc handler = options.GetHandler(statusCode); if (handler != null) { await handler(env); } } }); }