问题
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?
回答1:
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.
回答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.
回答3:
If I stop and restarted IIS on my local workstation, I am able to resolve this issue.
回答4:
This blog posting describes the issue well:
http://professionalaspnet.com/archive/2007/04/09/Unable-to-cast-object-of-type-_2700_ASP.masterpage_5F00_master_2700_-to-type-_2700_ASP.masterpage_5F00_master_27002E00_.aspx
回答5:
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.
来源:https://stackoverflow.com/questions/512064/unable-to-cast-object-of-type-x-to-type-x-asp-net