Skip to content

Blazor API Review: Built-in components #12655

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Router AppAssembly="typeof(Program).Assembly">
<NotFoundContent>
<NotFound>
<p>Sorry, there's nothing at this address.</p>
</NotFoundContent>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,9 @@ public partial class PageDisplay : Microsoft.AspNetCore.Components.IComponent
{
public PageDisplay() { }
[Microsoft.AspNetCore.Components.ParameterAttribute]
public Microsoft.AspNetCore.Components.RenderFragment AuthorizingContent { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public Microsoft.AspNetCore.Components.RenderFragment Authorizing { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
[Microsoft.AspNetCore.Components.ParameterAttribute]
public Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.AuthenticationState> NotAuthorizedContent { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.AuthenticationState> NotAuthorized { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
[Microsoft.AspNetCore.Components.ParameterAttribute]
public System.Type Page { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
[Microsoft.AspNetCore.Components.ParameterAttribute]
Expand Down Expand Up @@ -651,11 +651,11 @@ public Router() { }
[Microsoft.AspNetCore.Components.ParameterAttribute]
public System.Reflection.Assembly AppAssembly { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
[Microsoft.AspNetCore.Components.ParameterAttribute]
public Microsoft.AspNetCore.Components.RenderFragment AuthorizingContent { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public Microsoft.AspNetCore.Components.RenderFragment Authorizing { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
[Microsoft.AspNetCore.Components.ParameterAttribute]
public Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.AuthenticationState> NotAuthorizedContent { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public Microsoft.AspNetCore.Components.RenderFragment<Microsoft.AspNetCore.Components.AuthenticationState> NotAuthorized { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
[Microsoft.AspNetCore.Components.ParameterAttribute]
public Microsoft.AspNetCore.Components.RenderFragment NotFoundContent { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public Microsoft.AspNetCore.Components.RenderFragment NotFound { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } [System.Runtime.CompilerServices.CompilerGeneratedAttribute]set { } }
public void Attach(Microsoft.AspNetCore.Components.RenderHandle renderHandle) { }
public void Dispose() { }
System.Threading.Tasks.Task Microsoft.AspNetCore.Components.IHandleAfterRender.OnAfterRenderAsync() { throw null; }
Expand Down
4 changes: 2 additions & 2 deletions src/Components/Components/src/Auth/AuthorizeViewCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
}
else if (isAuthorized)
{
var authorizedContent = Authorized ?? ChildContent;
builder.AddContent(1, authorizedContent?.Invoke(currentAuthenticationState));
var authorized = Authorized ?? ChildContent;
builder.AddContent(1, authorized?.Invoke(currentAuthenticationState));
}
else
{
Expand Down
14 changes: 7 additions & 7 deletions src/Components/Components/src/PageDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public class PageDisplay : IComponent
/// The content that will be displayed if the user is not authorized.
/// </summary>
[Parameter]
public RenderFragment<AuthenticationState> NotAuthorizedContent { get; set; }
public RenderFragment<AuthenticationState> NotAuthorized { get; set; }

/// <summary>
/// The content that will be displayed while asynchronous authorization is in progress.
/// </summary>
[Parameter]
public RenderFragment AuthorizingContent { get; set; }
public RenderFragment Authorizing { get; set; }

/// <inheritdoc />
public void Attach(RenderHandle renderHandle)
Expand Down Expand Up @@ -109,14 +109,14 @@ private RenderFragment WrapInAuthorizeViewCore(RenderFragment pageFragment)
}

// Some authorization data exists, so we do need to wrap the fragment
RenderFragment<AuthenticationState> authorizedContent = context => pageFragment;
RenderFragment<AuthenticationState> authorized = context => pageFragment;
return builder =>
{
builder.OpenComponent<AuthorizeViewWithSuppliedData>(0);
builder.AddAttribute(1, nameof(AuthorizeViewWithSuppliedData.AuthorizeDataParam), authorizeData);
builder.AddAttribute(2, nameof(AuthorizeViewWithSuppliedData.Authorized), authorizedContent);
builder.AddAttribute(3, nameof(AuthorizeViewWithSuppliedData.NotAuthorized), NotAuthorizedContent ?? DefaultNotAuthorizedContent);
builder.AddAttribute(4, nameof(AuthorizeViewWithSuppliedData.Authorizing), AuthorizingContent);
builder.AddAttribute(2, nameof(AuthorizeViewWithSuppliedData.Authorized), authorized);
builder.AddAttribute(3, nameof(AuthorizeViewWithSuppliedData.NotAuthorized), NotAuthorized ?? DefaultNotAuthorized);
builder.AddAttribute(4, nameof(AuthorizeViewWithSuppliedData.Authorizing), Authorizing);
builder.CloseComponent();
};
}
Expand All @@ -133,7 +133,7 @@ private class AuthorizeViewWithSuppliedData : AuthorizeViewCore

// There has to be some default content. If we render blank by default, developers
// will find it hard to guess why their UI isn't appearing.
private static RenderFragment DefaultNotAuthorizedContent(AuthenticationState authenticationState)
private static RenderFragment DefaultNotAuthorized(AuthenticationState authenticationState)
=> builder => builder.AddContent(0, "Not authorized");
}
}
26 changes: 13 additions & 13 deletions src/Components/Components/src/Routing/Router.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ public class Router : IComponent, IHandleAfterRender, IDisposable
/// <summary>
/// Gets or sets the type of the component that should be used as a fallback when no match is found for the requested route.
/// </summary>
[Parameter] public RenderFragment NotFoundContent { get; set; }
[Parameter] public RenderFragment NotFound { get; set; }

/// <summary>
/// The content that will be displayed if the user is not authorized.
/// </summary>
[Parameter] public RenderFragment<AuthenticationState> NotAuthorizedContent { get; set; }
[Parameter] public RenderFragment<AuthenticationState> NotAuthorized { get; set; }

/// <summary>
/// The content that will be displayed while asynchronous authorization is in progress.
/// </summary>
[Parameter] public RenderFragment AuthorizingContent { get; set; }
[Parameter] public RenderFragment Authorizing { get; set; }

private RouteTable Routes { get; set; }

Expand Down Expand Up @@ -94,8 +94,8 @@ protected virtual void Render(RenderTreeBuilder builder, Type handler, IDictiona
builder.OpenComponent(0, typeof(PageDisplay));
builder.AddAttribute(1, nameof(PageDisplay.Page), handler);
builder.AddAttribute(2, nameof(PageDisplay.PageParameters), parameters);
builder.AddAttribute(3, nameof(PageDisplay.NotAuthorizedContent), NotAuthorizedContent);
builder.AddAttribute(4, nameof(PageDisplay.AuthorizingContent), AuthorizingContent);
builder.AddAttribute(3, nameof(PageDisplay.NotAuthorized), NotAuthorized);
builder.AddAttribute(4, nameof(PageDisplay.Authorizing), Authorizing);
builder.CloseComponent();
}

Expand All @@ -120,14 +120,14 @@ private void Refresh(bool isNavigationIntercepted)
}
else
{
if (!isNavigationIntercepted && NotFoundContent != null)
if (!isNavigationIntercepted && NotFound != null)
{
Log.DisplayingNotFoundContent(_logger, locationPath, _baseUri);
Log.DisplayingNotFound(_logger, locationPath, _baseUri);

// We did not find a Component that matches the route.
// Only show the NotFoundContent if the application developer programatically got us here i.e we did not
// Only show the NotFound if the application developer programatically got us here i.e we did not
// intercept the navigation. In all other cases, force a browser navigation since this could be non-Blazor content.
_renderHandle.Render(NotFoundContent);
_renderHandle.Render(NotFound);
}
else
{
Expand Down Expand Up @@ -159,18 +159,18 @@ Task IHandleAfterRender.OnAfterRenderAsync()

private static class Log
{
private static readonly Action<ILogger, string, string, Exception> _displayingNotFoundContent =
LoggerMessage.Define<string, string>(LogLevel.Debug, new EventId(1, "DisplayingNotFoundContent"), $"Displaying {nameof(NotFoundContent)} because path '{{Path}}' with base URI '{{BaseUri}}' does not match any component route");
private static readonly Action<ILogger, string, string, Exception> _displayingNotFound =
LoggerMessage.Define<string, string>(LogLevel.Debug, new EventId(1, "DisplayingNotFound"), $"Displaying {nameof(NotFound)} because path '{{Path}}' with base URI '{{BaseUri}}' does not match any component route");

private static readonly Action<ILogger, Type, string, string, Exception> _navigatingToComponent =
LoggerMessage.Define<Type, string, string>(LogLevel.Debug, new EventId(2, "NavigatingToComponent"), "Navigating to component {ComponentType} in response to path '{Path}' with base URI '{BaseUri}'");

private static readonly Action<ILogger, string, string, string, Exception> _navigatingToExternalUri =
LoggerMessage.Define<string, string, string>(LogLevel.Debug, new EventId(3, "NavigatingToExternalUri"), "Navigating to non-component URI '{ExternalUri}' in response to path '{Path}' with base URI '{BaseUri}'");

internal static void DisplayingNotFoundContent(ILogger logger, string path, string baseUri)
internal static void DisplayingNotFound(ILogger logger, string path, string baseUri)
{
_displayingNotFoundContent(logger, path, baseUri, null);
_displayingNotFound(logger, path, baseUri, null);
}

internal static void NavigatingToComponent(ILogger logger, Type componentType, string path, string baseUri)
Expand Down
34 changes: 17 additions & 17 deletions src/Components/Components/test/Auth/AuthorizeViewTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public void RendersNothingIfNotAuthorized()
}

[Fact]
public void RendersNotAuthorizedContentIfNotAuthorized()
public void RendersNotAuthorizedIfNotAuthorized()
{
// Arrange
var authorizationService = new TestAuthorizationService();
var renderer = CreateTestRenderer(authorizationService);
var rootComponent = WrapInAuthorizeView(
notAuthorizedContent:
notAuthorized:
context => builder => builder.AddContent(0, $"You are not authorized, even though we know you are {context.User.Identity.Name}"));
rootComponent.AuthenticationState = CreateAuthenticationState("Nellie");

Expand Down Expand Up @@ -88,7 +88,7 @@ public void RendersNotAuthorizedContentIfNotAuthorized()
}

[Fact]
public void RendersNothingIfAuthorizedButNoChildContentOrAuthorizedContentProvided()
public void RendersNothingIfAuthorizedButNoChildContentOrAuthorizedProvided()
{
// Arrange
var authorizationService = new TestAuthorizationService();
Expand Down Expand Up @@ -152,14 +152,14 @@ public void RendersChildContentIfAuthorized()
}

[Fact]
public void RendersAuthorizedContentIfAuthorized()
public void RendersAuthorizedIfAuthorized()
{
// Arrange
var authorizationService = new TestAuthorizationService();
authorizationService.NextResult = AuthorizationResult.Success();
var renderer = CreateTestRenderer(authorizationService);
var rootComponent = WrapInAuthorizeView(
authorizedContent: context => builder =>
authorized: context => builder =>
builder.AddContent(0, $"You are authenticated as {context.User.Identity.Name}"));
rootComponent.AuthenticationState = CreateAuthenticationState("Nellie");

Expand Down Expand Up @@ -235,13 +235,13 @@ public void RespondsToChangeInAuthorizationState()
}

[Fact]
public void ThrowsIfBothChildContentAndAuthorizedContentProvided()
public void ThrowsIfBothChildContentAndAuthorizedProvided()
{
// Arrange
var authorizationService = new TestAuthorizationService();
var renderer = CreateTestRenderer(authorizationService);
var rootComponent = WrapInAuthorizeView(
authorizedContent: context => builder => { },
authorized: context => builder => { },
childContent: context => builder => { });

// Act/Assert
Expand All @@ -260,7 +260,7 @@ public void RendersNothingUntilAuthorizationCompleted()
var renderer = CreateTestRenderer(authorizationService);
renderer.OnUpdateDisplayComplete = () => { @event.Set(); };
var rootComponent = WrapInAuthorizeView(
notAuthorizedContent:
notAuthorized:
context => builder => builder.AddContent(0, "You are not authorized"));
var authTcs = new TaskCompletionSource<AuthenticationState>();
rootComponent.AuthenticationState = authTcs.Task;
Expand Down Expand Up @@ -293,7 +293,7 @@ public void RendersNothingUntilAuthorizationCompleted()
}

[Fact]
public void RendersAuthorizingContentUntilAuthorizationCompleted()
public void RendersAuthorizingUntilAuthorizationCompleted()
{
// Arrange
var @event = new ManualResetEventSlim();
Expand All @@ -302,8 +302,8 @@ public void RendersAuthorizingContentUntilAuthorizationCompleted()
var renderer = CreateTestRenderer(authorizationService);
renderer.OnUpdateDisplayComplete = () => { @event.Set(); };
var rootComponent = WrapInAuthorizeView(
authorizingContent: builder => builder.AddContent(0, "Auth pending..."),
authorizedContent: context => builder => builder.AddContent(0, $"Hello, {context.User.Identity.Name}!"));
authorizing: builder => builder.AddContent(0, "Auth pending..."),
authorized: context => builder => builder.AddContent(0, $"Hello, {context.User.Identity.Name}!"));
var authTcs = new TaskCompletionSource<AuthenticationState>();
rootComponent.AuthenticationState = authTcs.Task;

Expand Down Expand Up @@ -447,9 +447,9 @@ public void RejectsNonemptyScheme()

private static TestAuthStateProviderComponent WrapInAuthorizeView(
RenderFragment<AuthenticationState> childContent = null,
RenderFragment<AuthenticationState> authorizedContent = null,
RenderFragment<AuthenticationState> notAuthorizedContent = null,
RenderFragment authorizingContent = null,
RenderFragment<AuthenticationState> authorized = null,
RenderFragment<AuthenticationState> notAuthorized = null,
RenderFragment authorizing = null,
string policy = null,
string roles = null,
object resource = null)
Expand All @@ -458,9 +458,9 @@ private static TestAuthStateProviderComponent WrapInAuthorizeView(
{
builder.OpenComponent<AuthorizeView>(0);
builder.AddAttribute(1, nameof(AuthorizeView.ChildContent), childContent);
builder.AddAttribute(2, nameof(AuthorizeView.Authorized), authorizedContent);
builder.AddAttribute(3, nameof(AuthorizeView.NotAuthorized), notAuthorizedContent);
builder.AddAttribute(4, nameof(AuthorizeView.Authorizing), authorizingContent);
builder.AddAttribute(2, nameof(AuthorizeView.Authorized), authorized);
builder.AddAttribute(3, nameof(AuthorizeView.NotAuthorized), notAuthorized);
builder.AddAttribute(4, nameof(AuthorizeView.Authorizing), authorizing);
builder.AddAttribute(5, nameof(AuthorizeView.Policy), policy);
builder.AddAttribute(6, nameof(AuthorizeView.Roles), roles);
builder.AddAttribute(7, nameof(AuthorizeView.Resource), resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

<CascadingAuthenticationState>
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly>
<AuthorizingContent>Authorizing...</AuthorizingContent>
<NotAuthorizedContent>
<Authorizing>Authorizing...</Authorizing>
<NotAuthorized>
<div id="auth-failure">
Sorry, @(context.User.Identity.Name ?? "anonymous"), you're not authorized.
</div>
</NotAuthorizedContent>
</NotAuthorized>
</Router>
</CascadingAuthenticationState>

Expand Down
1 change: 0 additions & 1 deletion src/Components/test/testassets/BasicTestApp/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<option value="BasicTestApp.RenderFragmentToggler">Render fragment renderer</option>
<option value="BasicTestApp.ReorderingFocusComponent">Reordering focus retention</option>
<option value="BasicTestApp.RouterTest.TestRouter">Router</option>
<option value="BasicTestApp.RouterTest.TestRouterWithoutNotFoundContent">Router without NotFoundContent</option>
<option value="BasicTestApp.RouterTest.UriHelperComponent">UriHelper Test</option>
<option value="BasicTestApp.SvgComponent">SVG</option>
<option value="BasicTestApp.SvgWithChildComponent">SVG with child component</option>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@using Microsoft.AspNetCore.Components.Routing
<Router AppAssembly=typeof(BasicTestApp.Program).Assembly>
<NotFoundContent>
<NotFound>
<div id="test-info">Oops, that component wasn't found!</div>
</NotFoundContent>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@using Microsoft.AspNetCore.Components.Routing
Router component
<Router AppAssembly="System.Reflection.Assembly.GetAssembly(typeof(RouterContainer))">
<NotFoundContent>
<NotFound>
<p>Route not found</p>
</NotFoundContent>
</NotFound>
</Router>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Startup).Assembly">
<NotFoundContent>
<NotFound>
<p>Sorry, there's nothing at this address.</p>
</NotFoundContent>
</NotFound>
</Router>
</CascadingAuthenticationState>