You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
using System;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace Mvc.Server.Extensions
|
|
{
|
|
public static class AppBuilderExtensions
|
|
{
|
|
public static IApplicationBuilder UseWhen(this IApplicationBuilder app,
|
|
Func<HttpContext, bool> condition, Action<IApplicationBuilder> configuration)
|
|
{
|
|
if (app == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(app));
|
|
}
|
|
|
|
if (condition == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(condition));
|
|
}
|
|
|
|
if (configuration == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(configuration));
|
|
}
|
|
|
|
var builder = app.New();
|
|
configuration(builder);
|
|
|
|
return app.Use(next =>
|
|
{
|
|
builder.Run(next);
|
|
|
|
var branch = builder.Build();
|
|
|
|
return context =>
|
|
{
|
|
if (condition(context))
|
|
{
|
|
return branch(context);
|
|
}
|
|
|
|
return next(context);
|
|
};
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|