ASP.net c# Masterpage problem

0 votes
asked Sep 13, 2010 by tom-gullen

I'm trying to make my masterpage work with my content page, allowing the content page to access the master page controls. I get the error:

Parser Error Message: The 'mastertype' directive must have exactly one attribute: TypeName or VirtualPath

This is on the lines:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewProduct.aspx.cs" Inherits="AlphaPackSite.viewProduct"
    MasterPageFile="MasterPages/Main.master"
    title="Hi there!"
%>
<%@ MasterType TypeName="Main" VirtualPath="MasterPages/Main.master" %>

My master page is:

namespace AlphaPackSite
{
    public partial class Main : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

I'm a bit confused with namespaces and such so may have got them wrong, but all pages are in the same namespace now I beleive.

Update

When I turn it to:

<%@ MasterType VirtualPath="MasterPages/Main.master" %>

I keep getting the error:

Compiler Error Message: CS0030: Cannot convert type 'AlphaPackSite.Main' to 'ASP.masterpages_main_master'

Source Error:

Line 147:        public new ASP.masterpages_main_master Master {
Line 148:            get {
Line 149:                return ((ASP.masterpages_main_master)(base.Master));
Line 150:            }
Line 151:        }

5 Answers

0 votes
answered Sep 13, 2010 by david-001

Try:

<%@ MasterType TypeName="AlphaPackSite.Main"  %>
0 votes
answered Sep 13, 2010 by michael-shimmins

As the error says, @MasterType expects only one parameter. Try just:

<%@ MasterType VirtualPath="MasterPages/Main.master" %>

See http://msdn.microsoft.com/en-us/library/ms228274.aspx for more info.

0 votes
answered Sep 13, 2010 by sidharth-panwar

Unless you want to keep a reference of your master page in the current page (in which this code is written), I'd suggest you remove the <%@ MasterType VirtualPath="MasterPages/Main.master" %> line.

This line provides a way to access your page's master page (such as when you've to change a label on the master page or the menu needs to add a few more items etc.). If the content of your master page does not require any changes/updates from your content page there's not need to use the MasterType tag. Because by using both MasterPageFile="MasterPages/Main.master and MasterType, you're confusing the compiler (even though the master page is same).

Update
If you have to keep the MasterType tag, then remove the MasterPageFile="MasterPages/Main.master attribute from the page directive.

0 votes
answered Sep 13, 2010 by prashant-lakhlani

(1) comment out MasterType directive and compile the web site (2) uncomment the masterType directive and place it with either type name or virtual path, both will throw error

Reason for commenting and uncommenting: Logically, when website fails to build, it will not have AlphaPackSite.Main created and hence will throw error but once you comment it out, and there are not other errors in the code, you will hopefully get the type in your bin!

So there are more chances to work with comment > compile > uncomment kind of things
reference for MasterType: http://msdn.microsoft.com/en-us/library/ms228274.aspx

0 votes
answered Sep 18, 2015 by semdeveloper

I saw this question while searching for another answer and figured I would provide the answer quickly in case other people have this issue.

The problem is that you do not want to use a virtual path in the master name call. Instead you need to use TypeName and add in underscores for your path. Here is the correct tag based on your path:

<%@ MasterType TypeName="AlphaPackSite_Main" %>
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...