.net core 2.1 reusing _Layout throughout various projects
Asked Answered
S

1

3

I am trying to reuse the same _Layout.cshtml file, throughout multiple .net core 2.1 Razor Page projects. Currently I have a Razor Page Class Library (Common shared project) that is where the _Layout.cshtml file will be located. Along with the layout file there are css and js files that should also be accessible to all other projects (I was able to make this work).

secondary projects will have references to the Common shared project and should be able to utilize the _Layout.cshtml file from it.

My issue is that currently it is not loading the common _layout file when I am accessing pages from these projects, instead it loads its own layout file. If I try deleting this file it throws the error that it cannot find the layout file. However, if I load a page contained inside of the Common shared project it does load its own _layout.

I have used the following tutorials to get where I am at but I could not figure out how to make the secondary projects load the layout in the Common shared project.

Including Static Resources In Razor Class Libraries In ASP.NET Core

Can Razor Class Library pack static files (js, css etc) too

Reuse UI With Razor Class Libraries (RCL) In ASP.NET Core

Please keep in mind that 2 of these articles are based on sharing the css and js files (which did work) but I have read up on other examples for the layout as well and have had no luck (currently I don't have the links to those other articles)

As far as what I have coded I'm currently experimenting on a sample project and its the same as the first article (Including Static Resources In Razor Class Libraries In ASP.NET Core).

UPDATE

Utilizing the 3rd link (Reuse UI With Razor Class Libraries (RCL) In ASP.NET Core) I was able to accomplish my goal. However, I was using this same strategy previously and it was not working. I am going to try playing around with this a bit more. If I have any other updates I will post them.

Syllabify answered 9/11, 2018 at 18:47 Comment(0)
S
3

So apparently since I was working off of the Including Static Resources In Razor Class Libraries In ASP.NET Core link the example left the default razor page class library structure containing the Area/MyFeatures folders. I modified this to work like the last link Reuse UI With Razor Class Libraries (RCL) In ASP.NET Core where the Common shared project is structured the same way that the web application project is (no Area or MyFeatures folder). I also added:

@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers

to the end of my _ViewStart.cshtml located in the Common shared project.

Also just as a note you must delete the _layout.cshtml and _viewstart.cshtml from the web application otherwise it will override the shared project layout.

Anyhow this seems to have fixed the issue and it is now working.

Syllabify answered 9/11, 2018 at 19:35 Comment(5)
Did you find official documentation anywhere regarding the web site overwriting the shared project files of _viewimports and _viewstart?Dad
@Dad Hi, this was from a while back so I don't have any of my old references but I did a quick search for you. Here is a link (refer to section Running Code Before Each View): learn.microsoft.com/en-us/aspnet/core/mvc/views/… While the link I am providing does not explicitly talk about this situation, it does mention that they load in a hierarchical manner. If I am not mistaken this is why it has this behavior (loading from the principal application before attempting to load from a referenced project).Syllabify
Also note this link is from .net core 5.0 but I don't think this hierarchical loading structure changed between these versions... Also it speaks of one of these files but I'm pretty sure it applies the same rule to all of them.Syllabify
Unless I'm reading that wrong, that is saying that it should work how I want? I read it as though it is going run each of the matching files in the hierarchy. Versus only the first match. My question was here: #65558190 but reading those docs you sent, I see "@using: all are included; duplicates are ignored" from this section learn.microsoft.com/en-us/aspnet/core/mvc/views/…Dad
@Dad You can actually leave the _viewimports.cshtml in your site project. I will correct that in my answer. The files I had to delete are only _layout.cshtml and _viewstart.cshtml. So you should be able to import on your site and then it will fall back onto the shared projects _viewimport.cshtml file to load the rest of the directives. The statement on the site "@using: all are included; duplicates are ignored" is just saying that it will load all usings from both site and shared project levels but if you have 2 similar using statements on both projects the shared ones will be ignored.Syllabify

© 2022 - 2024 — McMap. All rights reserved.