I was wondering if there is a way to share the _layout.cshtml file across all projects. This way if I ever wanted to make a change to our companies page layout I only have to change it in one place. From the research I've seen so far people have made mention of possibly creating a .dll file. Also I'm not talking about areas in mvc. What I want is for all my separate applications to reference one shared layout page instead of each application having it's own layout.
2 Answers
Once pattern (which I've been involved in the implementation of) is to make a Nuget package containing the layout file (and anything else you want to share in common between all your sites, such as other CSS files, error handling routines, etc), and installing the package (from a private feed) into each project which requires it, thus overwriting the default layout file).
The source of the Nuget package is itself a C# MVC project with a custom nuget config which makes sure that it includes just the necessary content from the C# project into the distributable nuget file.
Apart from ease of installation and separation of concerns, another big advantage of distributing via Nuget is that if you make changes to the source files and update the package, each application which has the package installed can choose when would be a good time to update to the latest version (if at all), so you don't have issues where if you change one thing you have to change everything which depends on it all at the same time (For instance, the Add as Link option in Slappywag's answer would suffer from that problem - if you changed the layout file in that scenario it would immediately propagate to all the projects which use it, so if you wanted to make any other changes in that project you'd have to deal with any implications of the layout change before you could publish your other changes, which might not be desirable/convenient at the time.)

- 57,178
- 14
- 51
- 63
-
Did you follow any tutorial when creating this? – smuldr Jan 25 '19 at 23:02
-
@crmckain No it was the brainchild of me and another colleague – ADyson Jan 26 '19 at 08:25
-
Is there anything in particular you read when doing your research? – smuldr Jan 27 '19 at 02:37
-
@crmckain Nothing specific which would cover the whole topic no. From what I recall we looked up how to manually specify particular files to be placed in a nuget (rather than just relying on the build output of the C# project), and how to use [source code transforms](https://learn.microsoft.com/en-us/nuget/create-packages/source-and-config-file-transformations) to inject values into C# files at install-time. The rest I think we just figured out as we went along. There's nothing particularly odd or non-standard in the solution. – ADyson Jan 28 '19 at 19:50
The way I've achieved this in the past is to use 'add as link'.
Firstly, add your layout files to a new library which can be shared between your different projects. Then in each of your projects which you want to use the layout page add the layout page as a link:
- In solution explorer right click on the folder in whcih you want to place your layout file.
- Click add existing, and select the layout file you want to use.
- Click the little arrow on the right hand side of the 'Add' button, and select 'Add as link' from the dropdown. Your layout file will now be in place.
- Depending on how your project is set up you may need to follow the steps in this answer to ensure your linked files are copied at build time. You may also want to make sure the copied files are being ignored by your source control.
- Update your
ViewStart
to use the new layout file.
Any changes you need to make to the layout can be made to the original file, and on build changes will be picked up by your projects.

- 1,143
- 1
- 17
- 27