Template vs component

Templates and components serve very similar purposes - to reduce the amount of copy and pasting code that you have to do. However, templates have a significant downside, which is that pages created with a template will not be updated when the template is updated. So why don't we just use components instead? It can be difficult to use components instead of templates, as with templates you can have spaces to write HTML/Razor, but with components you have to pass all data in through a C# object. If your page is simple enough you might just want to make a component that has the basic structure of a page and only have that component on each page, but in many cases that won't be possible. In that case, try to make as many parts of the page in components, so that you can change it if necessary.

Using ViewData

ViewData is a C# object that is passed from your page to your layout. The default layout uses ViewData for setting the title of the page in the head of the resulting HTML file, but it can be used for much more. However, don't use ViewData to communicate between a component and a page when a parameter in the Include method would work.

Here's an example of how to use ViewData in your page:

@{ ViewData["Title"] = "Index"; }

And here's how you can use it in your layout:

<title>@ViewData["Title"]</title>