How to know at render time what partials are being used in a Razor page in ASPNET Core? - asp.net-core

I have an ASPNET Core (.net6) project with a main page layout that automatically includes styles and scripts in the page bases on the page file name. Example: if you have a page called me/details (the file being me/details.cshtml) and there is a file called wwwroot/me/details.css, a reference to it will be included by my layout page inthe head of the html. The same happens for script files, but at the end of the body
I wanted to do the same for partials views. I want to include specific styles and script file references in the html for any partial views being rendered in the page, without extra code in the partials. Is there any way to know at render time, or at least at the middleware level, what partial views are being used in a page?

Related

How to render multiple root elements in Xamarin forms?

I want to render two pages in Xamarin. One is the main page and the other one is a footer page- they both are .xaml files. The only problem is that the footer uses a ThriveGmbH.BottomNavigationBar.XF imported NuGet class and the Main Page uses a content page class. When I try to use both classes inside a single script I get a "multiple root elements" error. Is it possible to render both pages or call the footer page inside of the main page? Thank you in advance.

what is the difference between razorview and razorlayout in aspnet core

I used razor pages and want to create a razor page partial view.
I saw two options one razor view and another razor layout, what is different between the two? Which is better for a partial view?
It doesn't really matter which template you use to create a partial for Razor Pages. In both cases, you will need to replace the existing content and rename the file. A partial is just a single Razor file that doesn't have an #page directive.
You could also use the ViewStart and View Imports templates. The Razor Page template could also be used, but you would also need to delete the PageModel class file that gets generated.
More about Partials in Razor Pages here: https://www.learnrazorpages.com/razor-pages/partial-pages

ASP.Net Core route template behaviour

I've seen in some sample code that a route template ("{id:int}") on top of razor page causes the links to that page to use another pattern:
https://localhost/Movies/Edit/6
instead of
https://localhost/Movies/Details?id=6
My question is how asp.net manages to change all the links to that pattern, does it know about that page before rendering it?
Does it collaborate with other pages when processing a page?
When the application first starts, a collection of attribute routes are built. The routes are built for any Razor file with an #page directive in the root Pages folder, and for any other routes that have been defined via PageRouteConventions.
When you use the Url helper to generate links, or the anchor tag helper (which uses the Url helper behind the scenes), the link that gets generated is based on the attribute route that was built for the page that you pass to the helper.
In attribute routing, route parameters are added as segments in the URL, which is why the values are not appended as query string values. If you prefer query strings, don't declare route values as part of the #page directive.
Run the dotnet publish -c Release command and take a look inside the bin/Release folder.
You will not find your .cshtml files with html in them. What happaned where did all the html go? And how does this relate to the question?
You gotta remember that cshtml will endup being your regular ol' c# and all that fancy razor templating syntax end's up being c#. This process has many names and transpilation is one of them performed by transpilers.
Okey so now that we can safely assume that when you have a Index.cshtml file it will get populated in to some sort of an object, let's call it RazorPage.cs this will just store all the configuration for this page. Now let's say this index page is living in a folder called Home now we can have a dictionary Dictionary<string, RazorPage> and let's say that the key will be "/Home/Index". Following along based on transpiled #page "{id:int}" syntax, it might generate a template string for the route and store that in the RazorPage in a RouteTemplate parameter.
So when you use asp-page tag helper it will find the correct RazorPage and it can know the template for the url, populating it with the values you provided.
I haven't seen the actual implementation this is just my guess.
My question is how asp.net manages to change all the links to that pattern, does it know about that page before rendering it?
Yes it knows everything about the page at run time. Most likely the services.AddMvc() service takes care of loading in all the razor pages / views / controllers, at startup.
Does it collaborate with other pages when processing a page?
Highly likely no, unless you mean components/layouts/partials. It will however struggle to resolve a page if you have identical route for 2 pages.

Razor Pages Partial Views with routing

I'd like to have Razor Pages as partial views but also being able to get them separately as HTML fragments so I can update them with AJAX.
If I put a #page at the top of my partial view, it gets routed but its model object will be null. If I remove #page, the model will be valid however I can no longer GET the partial view separately.
How can I have the cshtml be routed and have a valid model at the same time? I'm on ASP.NET Core 2.1 and upgrading is unfortunately not an option.
You cannot. Just create a partial view with the HTML you're wanting to share, and then call that partial view in your Razor Page. Then, you can use the partial view directly when you need that.
_MyPartialView.cshtml
#model MyPartialViewModel
<div>Here's my HTML</div>
MyPage.cshtml
#page MyPageModel
<partial name="_MyPartialView" model="Model.MyPartialViewModelInstance" />
If I put a #page at the top of my partial view, it gets routed but its model object will be null. If I remove #page, the model will be valid however I can no longer GET the partial view separately.
Partial Pages or Views are Razor files containing snippets of HTML and server-side code to be included in any number of pages or layouts. Partial pages can be used to break up complex pages into smaller units, thereby reducing the complexity and allowing teams to work on different units concurrently.
Just like standard Razor pages, partial pages support the #model directive specifying the type for the partial's data model. All of the rendering methods have overloaded versions that take a model to be consumed in the partial.
Note that the partial page does not feature an#page directive. That would make the file a full Razor Page and will lead to a NullReferenceExceptionrelated to the model not being declared when the framework attempts to invoke it - because there is no associated PageModel file. You could refer to Partial Pages in a Razor Pages application.

Sitefinity widgets not showing on News Detail page

Inherited a Sitefinity website. There's a news list page, which I discovered is reused by the news detail pages to display content. If I update the list page, the changes are reflected on the detail pages.
Sometimes.
I have a content block that contains a "header" text - updating this in the list page is replicated across the details pages. Adding a javascript widget to the page to inject some custom javascript replicates across the details pages as well.
Adding a new content block or css widget does not replicate across the details pages.
Is there some rhyme or reason to this behavior that I'm missing?
My specifics:
I've successfully created widgets in the MVC several times now. I essentially need to add a new widget to just the news pages. Which seemed simple enough until I discovered that news pages are not individually created pages like... well, pages... but instead are just a content piece that is dynamically inserted in the news widget on the "parent" listing page. At least as far as I can tell that's how it appears to be working.
Adding my widget to the page didn't work, as I explained above. I then tried recreating it in the page itself using javascript, content block, and css widgets, at which time I discovered that the javascript is the only one making it to the details pages. I imagine this has to do with the way javascript widgets actually make it to the page - their placement is selected in advanced options, rather than simply appearing inline.
Sitefinity widgets go beyond presentation, and actually control routing.
As such content widgets (baseline or custom) have two 'modes' that they operate in: list and detail. Slugs for details are automatically generated in the following format.
/News
/News/{News-item-slug}
Of course, a list and a detail should look very different. To accommodate that, the widgets have two separate configurable templates.
So, add your custom html and javascript to the appropriate template to have it only apply in a given mode.