Unable to cast object of type 'X' to type 'X' - ASP.NET
Asked Answered
O

6

5

I am currently working with a ASP.NET Web site project in Visual Studio 2008 and everytime I make a change to code behind page for a user control and browse to page that is using the user contorl I get the following error:

Unable to cast object of type 'ASP.basepage_master' to type 'ASP.basepage_master'.

I have to rebuild my entire solution to order to resolve this error. Has anyone else experienced this error and do they know how to resolve it?

Oporto answered 4/2, 2009 at 16:14 Comment(0)
O
1

If I stop and restarted IIS on my local workstation, I am able to resolve this issue.

Oporto answered 21/4, 2009 at 15:25 Comment(1)
Scary that this worked. What if this happens in production? An IIS reset?Mulvey
T
8

Deleting your temporary ASP.NET files (everything in C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files) makes this go away... for a while.

Talkington answered 10/2, 2009 at 16:44 Comment(2)
I have to stop IIS in order to delete the temporary ASP.NET files, which resolves this error.Oporto
Awesome. Glad it worked. (cough Can I get an up-vote? cough)Talkington
A
2

That makes sense, from what I understand.

You're making a change to the code-behind. In order for those changes to take effect, you'd need to build that project so that the code-behind can be compiled into the DLL file for that project. When you change the code-behind, and don't build the project, you've essentially got two versions of that page, and ASP.Net can't resolve the two versions. So, when you compile the class, the changes are reconciled into the DLL.

Affront answered 5/2, 2009 at 4:47 Comment(2)
I am using the web site project model, not the web application project model in visual studio.Oporto
I still believe the concept is the same, aside from the building to a dll file. ASP.NET still needs to compile your source files, and it's using it's cached version to run your site, not your "working" version that is uncompiled. odetocode.com/Articles/417.aspxAffront
O
1

If I stop and restarted IIS on my local workstation, I am able to resolve this issue.

Oporto answered 21/4, 2009 at 15:25 Comment(1)
Scary that this worked. What if this happens in production? An IIS reset?Mulvey
S
0

Eurekaaa!! Got it... This one works...

When you grag drop the usercontrol or load it via the LoadControl method use a 'tilde' in its path. E.g. this.LoadControl("~/MyUserControl.ascx");

The same applied for a control dropped on a page. Add the tilde to the declarative path of the user control in the aspx page.

We can call this as a bug in VS2005 and is fixed in VS 2008.

Reply me if this dosen't work or needs more explanation. Alternatively, don't forget to mark this as answer.

Sori answered 19/1, 2010 at 7:24 Comment(0)
E
0

I had this same issue, and 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.

Here is the thread where my struggles played out:

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

Eudiometer answered 7/12, 2020 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.