I’m reading this tutorial(https://learn.microsoft.com/en-us/aspnet/web-pages/overview/ui-layouts-and-themes/3-creating-a-consistent-look) and trying to add header and footer into layout file.
But Visual Studio is giving an error when I build the project.
<!DOCTYPE html>
<html>
<head>
<title>Structured Content </title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
@RenderPage("~/Shared/_Header.cshtml")
<div id="main">
@RenderBody()
</div>
<div id="footer">
© 2012 Contoso Pharmaceuticals. All rights reserved.
</div>
</body>
</html>
Error message:
Error (active) CS0103 The name ‘RenderPage’ does not exist in the current context c:appsPagesShared_Layout1.cshtml 8
2
Answers
I think the solution is using
RenderPartial
.If you are using .Net Core, while following an old tutorial you’ll eventually end up scratching your head. Please note, that the tutorial you are using is referring to Asp.Net Web Pages, which was a nice technology released more then 10 years ago. I know it can get confusing, but nowadays verify, that these tutorials refer to at least .net core 3.1 and upwards. .Net 7 (now again without core) is about to be shipped.
You can use the Partial Tag Helper to render your partial:
When using an Html-Helper, it is considered best practices to use the async
Html.PartialAsync
method, though (The tag helper above is already ansynchronous):Feel free to have a deeper look into the docs:
https://learn.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-6.0
If you are looking for a more up-to-date tutorial please refer to https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/razor-pages-start?view=aspnetcore-6.0&tabs=visual-studio