locked
HttpUnhandledException Unable to cast object of type 'ASP.mcl959_master' to type 'mcl959.MCL959Master'. RRS feed

  • Question

  • User709251606 posted

    My website is old and has been working fine. After 3 years, I need to update some basic features.

    When I launch Visual Studio 2015 with my old ASP.NET project, I cannot seem to cast the Master page to the type of my class like the code used to do.

    Here is a look at the structure of my Master page MCL959.Master:

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="~/MCL959.Master.cs" Inherits="mcl959.MCL959Master" %>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">

    Here is a look at the structure of my code behind MCL959.Master.cs:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Net.Mail;
    using System.Net.Mime;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.Configuration;
    using System.Web.UI.WebControls;
    using jp2library;
    
    namespace mcl959
    {
        public partial class MCL959Master : System.Web.UI.MasterPage
        {
            private const String GUI_MESSAGE = "GuiMessage";
            public const String EMAIL_RSVP = "EMAIL_RESPONSE";
            public const String ROSTER_RESET = "ROSTER_RESET";
            public const String USER_PERSON = "USER_PERSON";
            private bool _runOnce;
            private Exception _emailError;
            private Personnel _members;
    
            protected void Page_Load(object sender, EventArgs e)       {

    Here is a look at an example of a child page Member.aspx:

    <%@ Page Title="Membership at MCL959" Language="C#" MasterPageFile="~/MCL959.Master" AutoEventWireup="true" CodeBehind="Member.aspx.cs" Inherits="mcl959.Member" %>
    <asp:Content ID="memberHead" ContentPlaceHolderID="head" runat="server">

    Here is a look at the structure of the child page code-behind for Member.aspx.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Web;
    using System.Web.Configuration;
    using System.Web.UI.WebControls;
    namespace mcl959
    {
        public partial class Member : System.Web.UI.Page
        {
            private int m_selectedIndex = -1;
            private MCL959Master m_master;
    
            protected void Page_Load(object sender, EventArgs e)       {           m_master = (MCL959Master)Master;

    The code above fails as soon as it attempts to cast m_master using (MCL959Master)Master:

    HttpUnhandledException Unable to cast object of type 'ASP.mcl959_master' to type 'mcl959.MCL959Master'.
    StackTrace:
       at System.Web.UI.Page.HandleError(Exception e)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
       at System.Web.UI.Page.ProcessRequest()
       at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
       at System.Web.UI.Page.ProcessRequest(HttpContext context)
       at ASP.member_aspx.ProcessRequest(HttpContext context) in c:\Users\jp2code\AppData\Local\Temp\Temporary ASP.NET Files\root\2e1f2f46\a2c9b529\App_Web_udjygg41.1.cs:line 0
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
    

    I went to the Temporary ASP.NET Files folder and deleted everything, but it just keeps coming back, but the error is still there.

    Why is Visual Studio getting confused and thinking that my Master page is 'ASP.mcl959_master' instead of what I have defined as 'mcl959.MCL959Master'?

    How do I fix it?

    Monday, November 23, 2020 2:38 AM

Answers

  • User709251606 posted

    I was finally able to get the site to build.  

    I had to create another Master Page for the ASP.NET project with a different name, copy and paste the content of the original into the new one, make all of my Child forms inherit from the new Master Page, and delete the old Master Page.  

    The website will run now. I don't know how the debugger got confused and started using a cached object that didn't exist. I tried everything to get it to release that cache (deleting the temp files under `C:\%USER%\AppData\Local` and under `C:\Windows\Microsoft.NET`, renaming the Master Page, renaming the Master class, making changes to the files to force them to reload, rebuilding the solution, add the `web.config` option `<compilation batch="false" debug="false"/>`) except deleting the Master Page.  

    Deleting the Master Page fixed it for me, but I don't understand why I had to do that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 7, 2020 3:55 PM

All replies

  • User-939850651 posted

    Hi jp2code,

    I went to the Temporary ASP.NET Files folder and deleted everything, but it just keeps coming back, but the error is still there.

    Why is Visual Studio getting confused and thinking that my Master page is 'ASP.mcl959_master' instead of what I have defined as 'mcl959.MCL959Master'?

    According to you description, I created a simple master page and content page to test your current situation, but I did not encounter this problem.

    so I have to guess that there is a master page named mcl959_master in the server directory you are looking at. Or did you rename the master page?

    And here is a similar case:

    https://stackoverflow.com/questions/15837523/unable-to-cast-object-of-type-masterpage-to-type-master

    Best regards,

    Xudong Peng

    Tuesday, November 24, 2020 8:17 AM
  • User709251606 posted

    Hi Xudong, and thanks for the reply.

    I can include a Try/Catch around setting the master page, but it just catches the error and my master page reference remains null and the page cannot access anything from it.

    You might be on to something. How do I find mcl959_master in the server directory? I have not configured anything in IIS, so it is running wherever Visual Studio defaults to.

    IIS Screenshot

    Also, I found an old thread that seems to describe exactly what I am going through, but it is from 2006 to 2010:

    https://forums.asp.net/t/954194.aspx

    There were a few hacks that some people found to be useful that I tried. The first was to edit the `web.config` file:

    <compilation batch="false" debug="false"/>

    That did not help.

    Another suggestion was to edit the master page in any way, even just by adding a blank line, "then delete it, rebuild your program and your code should work after that."

    That did not fix my issue either.

    There is a HotFix that Microsoft published that is supposed to solve this issue in the link below, but the link to the hotfix is no longer available.

    https://forums.asp.net/t/954194.aspx

    I suspect that a recent Microsoft Update closed a hole that Visual Studio 2015 was using, but I wouldn't know how to tell what update would have done that.

    If you have any other suggestions, I would really appreciate hearing those. The actual website currently has a bug in it, and I cannot even get the code to load to try and fix it.

    Tuesday, November 24, 2020 12:59 PM
  • User-939850651 posted

    Hi jp2code,

    I created the master page again and found a few differences, I'm not sure if this affects the results:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="MCL959.master.cs" Inherits="mcl959.MCL959" %>

    But in the code you provided, it seems to have been modified.

    And here is test result:

    Please check this case, I am not sure if it works:

    https://stackoverflow.com/questions/512064/unable-to-cast-object-of-type-x-to-type-x-asp-net.

    Best regards,

    Xudong Peng

    Wednesday, November 25, 2020 9:27 AM
  • User709251606 posted

    Did you find that in one of my Stackoverflow posts?

    I originally had my Master page class named MCL959 as you have pictured. One of the suggestions from questions with similar errors was to make sure there was no confusion in the page class name. My namespace was "mcl959" and the class name was "MCL959".

    For a few weeks now, I have had the Master page class renamed to "MCL959Master" in an effort to resolve any confusion.

    I have deleted all of the files in this folder

    c:\Users\jp2code\AppData\Local\Temp\Temporary ASP.NET Files\

    I have tried rebuilding the solution, and there are no errors when it compiles.

    Every time, I still get this same error whenever I try to open a page with code on it that accesses methods that I want available from the Master page.

    System.InvalidCastException

    Unable to cast object of type 'ASP.mcl959_master' to type 'mcl959.MCL959Master'.

    I have done a search in my project and on my computer, and I cannot find anything named "mcl959_master".

    I also cleared the Windows Logs from the Event Viewer (Application, Security, Setup, System, and Forwarded Events), ran the project in the Visual Studio 2019 debugger, attempted to load the Member page to get the crash, and came back to check the Windows Logs. Nothing!

    I also searched the system Registry for "mcl959_master", but it found nothing.

    Where is Visual Studio getting this type "ASP.mcl959_master" from?

    Wednesday, November 25, 2020 12:19 PM
  • User1120430333 posted

    The code you have shown is that you cannot make the cast  of type 'ASP.mcl959_master' to type 'mcl959.MCL959Master'. The object originates in ASP namespace the and the other object you are trying to cast the object to is in mcl959 namespace. .NET is not going to allow the casting of an object across two different namespace names.

    The best you could hope for is take Master to Object, which is the base object,   all .NET objects from and drop the namespace ASP off of it and the name.

    Object = Master;

    And then you can try to cast Object over to the type needed. You can try it.

    m_master = (MCL959Master)Object;
    Wednesday, November 25, 2020 10:23 PM
  • User709251606 posted

    Hi DA.

    But why did this happen? This is old code that I have not opened in a few years. Was the definition changed?

    I built this using the loosely-typed Page.Master property like in this example:

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/master-pages/interacting-with-the-master-page-from-the-content-page-cs

    (Search for "Using the Loosely-TypedPage.MasterProperty")

    I'm going to try the strongly-typed Page.Master version and see if that fixes shortly.

    Wednesday, November 25, 2020 11:37 PM
  • User709251606 posted

    I was finally able to get the site to build.  

    I had to create another Master Page for the ASP.NET project with a different name, copy and paste the content of the original into the new one, make all of my Child forms inherit from the new Master Page, and delete the old Master Page.  

    The website will run now. I don't know how the debugger got confused and started using a cached object that didn't exist. I tried everything to get it to release that cache (deleting the temp files under `C:\%USER%\AppData\Local` and under `C:\Windows\Microsoft.NET`, renaming the Master Page, renaming the Master class, making changes to the files to force them to reload, rebuilding the solution, add the `web.config` option `<compilation batch="false" debug="false"/>`) except deleting the Master Page.  

    Deleting the Master Page fixed it for me, but I don't understand why I had to do that.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 7, 2020 3:55 PM