-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Description
Is your feature request related to a problem? Please describe.
- I want my app to use the default layout when the user is logged in, and a different layout when they are not logged in.
- (This is because my default layout contains sidebar and nav bar options which are only relevant for logged in users).
- However, the
AuthorizeRouteView
component only allows setting aDefaultLayout
parameter. Using<LayoutView>
inside theNotAuthorized
parameter doesn't override this. (<LayoutView>
does work if noDefaultLayout
is set, but manually setting the layout on every page isn't ideal). - This can be currently be worked around by building a custom RouteView, as described in this previous issue.
- However, this seems like a common problem. As well as the issue mentioned above, here's another issue requiring the same thing, but where the developer was having trouble with the custom RouteView solution. A similar thing is being asked in this recent StackOverflow post.
Describe the solution you'd like
I'd like to be able to manually specify the layout for the NotAuthorized
content in an <AuthorizeRouteView>
, possibly using a NotAuthorizedLayout
parameter. e.g.
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" NotAuthorizedLayout="@typeof(CustomLayout)">
<NotAuthorized>
<StartPage />
</NotAuthorized>
</AuthorizeRouteView>
If there was also a AuthorizedLayout
parameter, this could allow for more advanced customisation of the layout in the NotAuthorized
content. e.g.
<!-- Use MainLayout when the user is authorized to view the page -->
<AuthorizeRouteView RouteData="@routeData" AuthorizedLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (context.User.Identity?.IsAuthenticated != true)
{
<!-- Use CustomLayout if the user isn't logged in -->
<LayoutView Layout="@typeof(CustomLayout)">
<StartPage />
</LayoutView>
}
else
{
<!-- Use ErrorLayout if the user is logged in, but not authorized -->
<LayoutView Layout="@typeof(ErrorLayout)">
<p role="alert">You are not authorized to access this resource.</p>
</LayoutView>
}
</NotAuthorized>
</AuthorizeRouteView>
(This currently doesn't work, because the LayoutView
doesn't override the DefaultLayout
parameter.)
Additional context
Many apps will want to use a different layout before the user is logged in. I think this is a common enough scenario that it would be worth adding this functionality to make it easier. The custom RouteView workaround described in the issue above didn't quite work for me; I needed to copy the entire source code for AuthorizeRouteView
and change the part which forces the DefaultLayout
for the NotAuthorized
routes. This solution works fine, although achieving this does seem more complicated than it needs to be.