-
Notifications
You must be signed in to change notification settings - Fork 191
Add UseWhenExtensions and UseWhenExtensionsTests #663
Conversation
Hi @tuespetre, I'm your friendly neighborhood .NET Foundation Pull Request Bot (You can call me DNFBOT). Thanks for your contribution! The agreement was validated by .NET Foundation and real humans are currently evaluating your PR. TTYL, DNFBOT; |
Nice, I had planned to add IMHO, using Here's my own version of |
Haha, reinvented wheels :) The 'Predicate' is just an alias for |
Sure, but this doesn't allow you to support path-specific middleware registrations. E.g // Create a new branch where the registered middleware will be executed only for non API calls.
app.UseWhen(context => !context.Request.Path.StartsWithSegments("/api"), branch => {
branch.UseCookieAuthentication(new CookieAuthenticationOptions { ... });
}); |
Okay, my bad. I had not realized you had added an alias at the top of the file 😄 |
Oops, GitHub took my brackets for HTML. I'm on my phone and don't seem to have the backtick key needed for an inline code span. That should have read 'Func(HttpContext,bool)' |
Defining custom aliases with common names (like On a related, an async overload might be nice too. |
/// <param name="predicate">Invoked with the request environment to determine if the branch should be taken</param> | ||
/// <param name="configuration">Configures a branch to take</param> | ||
/// <returns></returns> | ||
public static IApplicationBuilder UseWhen(this IApplicationBuilder app, Predicate predicate, Action<IApplicationBuilder> configuration) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This covers the if (condition) { }
scenario, but is there also a need for if (condition) { } else { }
or even if (condition) { } elseif (condition) { } elseif (condition) { } else { }
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (condition) { } else { }
can be done as if (condition) { } if (!condition) { }
, but it may be inefficient to evaluate the conditional twice.
if (condition) { } elseif (condition) else { }
can be done as if (condition) { } else { if (condition) { } else { } }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Tratcher as far as I see it this fills the need of the original issue, if the additional use case(s) ever become in demand they can be designed then...
Rebased and merged, thanks. |
Thanks! |
branchBuilder.Run(main); | ||
var branch = branchBuilder.Build(); | ||
|
||
return async context => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the async/await could have been removed here to save a state machine allocation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😱 true, true
Addresses #351