razor icon indicating copy to clipboard operation
razor copied to clipboard

Add support for @layout null in blazor

Open MarvinKlein1508 opened this issue 5 years ago • 12 comments

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

MarvinKlein1508 avatar Oct 23 '20 12:10 MarvinKlein1508

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.

ghost avatar Oct 26 '20 16:10 ghost

@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.

msschl avatar May 05 '21 09:05 msschl

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.

ghost avatar Jul 20 '21 17:07 ghost

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.

ghost avatar Nov 11 '21 17:11 ghost

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 avatar Feb 26 '23 19:02 mkArtak

@mkArtak I would stick with @layout null since it's the same syntax as in razor pages.

MarvinKlein1508 avatar Feb 26 '23 20:02 MarvinKlein1508

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.

SteveSandersonMS avatar Feb 27 '23 09:02 SteveSandersonMS

This seems super useful in order to support routable components that return just partial snippets of HTML.

Otherwise, it doesn't seem possible?

vyrotek avatar Jul 19 '23 04:07 vyrotek

Otherwise, it doesn't seem possible?

You can always render in an otherwise-blank layout, but I agree it's strange and inconvenient.

SteveSandersonMS avatar Jul 20 '23 15:07 SteveSandersonMS

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<>().

robodobdob avatar Jan 07 '24 04:01 robodobdob

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?

em-jones avatar Mar 12 '24 23:03 em-jones

any chance to support this in .NET 9?

WeihanLi avatar May 18 '24 14:05 WeihanLi

Is there any update on this?

Seems to be easy to implement for you guys.

Laftek avatar Jul 23 '24 10:07 Laftek

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.

Brandon689 avatar Aug 27 '24 06:08 Brandon689