By default SharePoint disabled the ability to run server side code in the pages. This means you can't have C# or VB.net code blocks in your layout pages, master pages, or web pages like we would have with a normal asp.net applications.
But it is possible to use the server-side code block within the SharePoint pages. To use the server-side code in C# or VB.Net within SharePoint custom pages, we need to write the C# or VB.Net code within the .aspx page inside the <script> tag as below:
<script runat="server">
protected void Page_Load(Object sender, EventArgs e)
{
Response.Write("Hello World");
}
</script>
Now open the web.config file of the SharePoint web site and locate PageParserPaths tag and modify this as below:
<PageParserPaths>
<PageParserPath VirtualPath="/*" CompilationMode="Always" AllowServerSideScript="true" />
</PageParserPaths>