Add support for @layout null in blazor
Is your feature request related to a problem? Please describe.
Sometimes you need no layout within a blazor app for some pages, e.g. for a print preview.
Adding @layout null to the component results in not compilable code.
This StackOverflow post does the trick but it should work by default just like in razor pages. https://stackoverflow.com/questions/59518988/disable-layout-for-page-under-blazor
Thanks for contacting us.
We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.
@SteveSandersonMS, @javiercn
I have a question regarding the @layout attribute.
In blazor the layout is set via @layout MyLayout. In razor or cshtml pages the layout is set via:
@{
Layout = "_Layout";
}
Is it possible to get a consitent pattern accross all? I other words, it would be nice to set the layout in cshtml or razor pages with the same @layout MyLayout attribute as in blazor. This would make files a bit cleaner. I often end up with this kind of code at the top of my pages:
@model LoginViewModel
@{
Layout = null;
}
...
To give some context why I end up having this code at the top of the pages: Our login, register, forgot password, mfa pages are all indipendent mobile first pages without the companies header and navigation bar and footer. Having just
@model XXX
@layout null
seams cleaner.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.
Either @layout null or @layout none or @nolayout I think can be options.
@SteveSandersonMS wouldn't this be a compiler ask or is this something to enable on Blazor side?
@mkArtak I would stick with @layout null since it's the same syntax as in razor pages.
wouldn't this be a compiler ask or is this something to enable on Blazor side?
Yes I think it would be a compiler feature.
This seems super useful in order to support routable components that return just partial snippets of HTML.
Otherwise, it doesn't seem possible?
Otherwise, it doesn't seem possible?
You can always render in an otherwise-blank layout, but I agree it's strange and inconvenient.
This would be nice to have with HTMX gaining traction. Being able to return just the output of a component without any wrapping markup would be nice.
The current approach I've used is the have endpoints that return RazorComponentResult<>().
This would be nice to have with HTMX gaining traction. Being able to return just the output of a component without any wrapping markup would be nice.
The current approach I've used is the have endpoints that return RazorComponentResult<>().
This is the exact problem I've been trying to solve... My solution looks like:
@* HXLayout.razor *@
@inherits Microsoft.AspNetCore.Components.LayoutComponentBase
@inject IHttpContextAccessor HttpContextAccessor;
@if (HttpContextAccessor.HttpContext != null
&& HttpContextAccessor.HttpContext.Request.Headers["HX-Request"] == "true")
{
@Body
}
else
{
<Main>
@Body
</Main>
}
My <Main> component is my "application shell" that I don't want rendering for htmx requests and I'm just passing children as RenderFragment parameters.
Anything that I expect to be an htmx endpoint gets the HXLayout layout applied.
Anybody see why this wouldn't be a good idea?
any chance to support this in .NET 9?
Is there any update on this?
Seems to be easy to implement for you guys.
Unless I missed something, rendering an empty layout is no solution because App.razor html head tags and content will still render. I just use API Controllers and RazorComponentResult.