Это мастер.aspx
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="~/Masters/Root21.master.cs" Inherits="Masters.Root21" %>
Это мастер.cs
namespace Masters
{
public partial class Root21: MasterPage
Это contentpage для мастера
...aspx
<%@ Page Title="" Language="C#" MasterPageFile="Masters/Root21.master" AutoEventWireup="true" CodeBehind="contentpage.aspx.cs" Inherits="contentpage" %>
и ..cs
public partial class contentpage: Page
{
protected void Page_Load(object Sender, EventArgs E)
{
Root21 root21 = (Root21)Page.Master;
а это ошибка
Unable to cast object of type 'ASP.masters_root21_master' to type 'Masters.Root21.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of type 'ASP.masters_root21_master' to type 'Masters.Root21.
А это вопрос:
какого чёрта??
Сделайте так: в App_Code, например в пространстве имён Masters, создайте класс Root21, унаследованный от MasterPage. А уже в файле master.cs определите класс, унаследованный от Masters.Root21. Тогда код var root21 = (Masters.Root21)Page.Master будет отрабатывать нормально.