For login, registration pages I'm using MVC .cshtml views. I have a Blazor component as my layout for Blazor pages and I want to apply it for mvc pages as well and avoid duplicate my layout.
Is that possible? if not what's the solution?
I َAppreciate any help and solution
This is covered in the docs for using razor components alongside MVC. Simply put, you don't use Blazor-style layouts. Rather, your MVC layout is utilized by the _Host.cshtml, and your routeable razor components are loaded within that.
Chris guided us to the doc to refer to (Thank you, Chris!). For those who still wish to try using Blazor-style layouts in MVC or Razor page projects, follow all instructions in the doc Chris pointed out and you can get a view like:
The above is rendered with a Blazor layout component (CustomLayout1.razor):
#inherits LayoutComponentBase
<div class="container-fluid border border-primary p-3">
<p>CustomLayout1</p>
<div class="container-fluid border border-danger p-3">
#Body
</div>
</div>
and a routable Blazor component which uses the custom layout (LayoutTest_Body.razor):
#page "/test"
#layout Views.Shared.CustomLayout1
<p>LayoutTest_Body</p>
As a sample for nested layouts, I added another Blazor layout component (CustomLayout2.razor) which is embedded into CustomLayout1:
#inherits LayoutComponentBase
#layout Views.Shared.CustomLayout1
<div class="container-fluid border border-warning p-3">
<p>CustomLayout2</p>
<div class="container-fluid border border-info p-3">
#Body
</div>
</div>
and changed the #layout of LayoutTest_Body.razor from CustomLayout1 to CustomLayout2:
#page "/test"
#layout Views.Shared.CustomLayout2
<p>LayoutTest_Body</p>
to get:
Please note that:
Blazor layouts can only be applied to routable components (see doc) like LayoutTest_Body.razor at the first line of which holds #page directive followed by a unique route string.
Above samples are based on AspNetCore5.0.
Only MVC images are shown, but the same for Razor pages except for a few differences in configurations and settings described in the doc Chris pointed out. Be careful to follow all the instructions.
As is often the case, templates provided by Visual Studio do not necessarily follow the way the Microsoft docs show (e.g. doc: app.MapBlazorHub(); , template: app.UseEndpoints(endpoints => { endpoints.MapBlazorHub(); });).
Passing parameters to layouts can be done through App.razor. In case of MVC project, (i) Create the parameter data and let public IActionResult Blazor() to pass it to _Host.cshtml via ViewBag, (ii) Let _Host.cshtml to pass the parameter data to App.razor by setting param-[paramname]="#ViewBag.xxxx", (iii) Receive the parameter at App.razor which in turn passes it to descendants as CascadingValue and finally (iv) Let any descendants to consume it as CascadingParameter. Passing non-serializable parameter to Blazor components is not allowed at the time of this posting, but creating a dictionary for layout settings and passing it in JSON format could be one solution for a centralized management of various layouts.
Related
I know I can render blazor component anywhere inside razor page using
Html.RenderComponentAsync() or <component type="typeof(Client.App)" render-mode="Static">
What I would like to achieve is being able to use regular blazor tag components with their ability to nest them with RenderFragment, for example:
<OutsideComponent>
<InsideComponent>
<div>Some content</div>
</InsideComponent>
</OutsideComponent>
At the same time I would like to keep all razor page functions like OnGet(), OnPost() handles.
Is that at all possible to merge those two approaches? I use render-mode="Static" and don't care about blazor interactivity, I just want my code separated to nestable components.
What part of sling adds the wrapping div and CSS classes when we do a sling include
When my component does a sling include
<sling:include path="/content/www/mysite/mycontent"/>
Translates to following in the markup
<div class="globalnavigationbar_ globalNavigationBar parbase">
<!-- start component markup -->
<nav class="globalnavbar hidden-xs hidden-sm">
<div>Component content</div>
</nav>
<!-- end component markup -->
</div>
I am interested in understanding what code in sling framework adds the wrapping div when doing a sling:include
<div class="globalnavigationbar_ globalNavigationBar parbase">
Tried to lookup[1] and follow the trail without success
[1] https://github.com/apache/sling-org-apache-sling-scripting-jsp-taglib/blob/master/src/main/java/org/apache/sling/scripting/jsp/taglib/IncludeTagHandler.java
The reason for trying to figure this out is trying to replace reference components with Apache SSI using Sling SDI.
Unfortunately, most of our current styles rely on those css classes and when using SDI the wrapping divs are missing.
Redoing our css classes is one option, trying to figure out alternatives if any
I don't think sling adds those tags. It is AEM's WCM which would do it. I do know that there is an IncludeOptions class which lets you override the decoration tag.
There are multiple ways to tell AEM to avoid decorating your components as detailed in this article. That being said, please be cautious with removing your decoration tags in author instance as you might have issues with getting your edit overlays working properly especially if you have components that have floats defined in the CSS.
<div id="Counter">
#(await Html.RenderComponentAsync<Counter>(new { IncrementAmount = 10 }))
</div>
why would i use this when i can just use
<counter IncrementAmount="10"/>
"When the page or view is rendered, components are prerendered at the same time."
https://learn.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0
thanks
The code sample you’ve quoted is used when you want to include a Blazor component into a MVC view or a Razor page. If you’re just writing a standard Blazor app then you wouldn’t need this syntax.
When using this syntax the component is going to be prerendered on the server and will become interactive once a SignalR connection has been established with the client browser.
Building a site using ASP.NET MVC 4.5, and using twitter's bootstrap-responsive. I noticed that on re-sizing the page, anything rendered by the _Layout view covers what I have rendered using the #RenderBody() statement. Is there any way to correct this?
I can't say for sure, but your problem probably isn't a consequence of #RenderBody. #RenderBody renders the content of your page that isn't in a section. The issue is probably a consequence of which twitter-bootstrap classes style the layout.
All I can do it point you to http://twitter.github.io/bootstrap/scaffolding.html (until the abbreviated html code is included).
I am using ASP.NET MVC 4 HTML.BeginForm and it creates a validation-summary-errors class as
below. This doesn't fit with my layout. Is there a way that I can make it put the
validation div class in a different place?
<form action="/User/Account/Login" method="post" novalidate="novalidate">
<div class="validation-summary-errors">
<ul>
<li>The user name or password provided is incorrect.</li>
</ul>
</div>
<fieldset>
<legend>Local Login</legend>
<ol>
<li>ation-summary-errors in a different position?
Well, sure, it's up to you to put the #Html.ValidationSummary() helper call which generates this markup wherever you want. As far as controlling the exact markup that this helper spits, that would be much more difficult because this helper offers you almost no control over the generated markup.
If you need to have more control you could write a custom helper.
You can use #Html.ValidationMessage("validation-errors") and put it in any HTML tag and then can use CSS to give it a style.