2

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:        }
Tom Gullen
  • 61,249
  • 84
  • 283
  • 456

5 Answers5

3

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.

Sidharth Panwar
  • 4,606
  • 6
  • 27
  • 36
  • This answer is wrong. You can not use the MasterType directive without the MasterPageFile attribute, this will lead to an error. The MasterType directive just lets you define the type of the used master, otherwise it will always default to System.Web.UI.MasterPage and you do not have access to your inherited class members and methods without type casting every call. See http://stackoverflow.com/questions/8946742/why-we-use-master-type – needfulthing Apr 03 '17 at 12:37
2

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.

Michael Shimmins
  • 19,961
  • 7
  • 57
  • 90
1

(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

Prashant Lakhlani
  • 5,758
  • 5
  • 25
  • 39
0

Try:

<%@ MasterType TypeName="AlphaPackSite.Main"  %>
David_001
  • 5,703
  • 4
  • 29
  • 55
-1

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" %>
Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
SEMDeveloper
  • 107
  • 1
  • 2
  • 5