visual-studio 无法将“X”类型的对象转换为“X”类型 - ASP.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/512064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:07:58  来源:igfitidea点击:

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

asp.netvisual-studio.net-3.5web

提问by Michael Kniskern

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:

我目前正在使用 Visual Studio 2008 中的 ASP.NET 网站项目,每次更改用户控件的代码隐藏页面并浏览到使用用户控制的页面时,我都会收到以下错误:

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

无法将“ASP.basepage_master”类型的对象转换为“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?

我必须重建我的整个解决方案才能解决这个错误。有没有其他人遇到过这个错误,他们知道如何解决吗?

采纳答案by Michael Kniskern

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

如果我在本地工作站上停止并重新启动 IIS,就可以解决此问题。

回答by teedyay

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.

删除临时 ASP.NET 文件(C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 中的所有内容)会使这种情况消失......一段时间。

回答by Carl

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.

您正在对代码隐藏进行更改。为了使这些更改生效,您需要构建该项目,以便可以将代码隐藏编译到该项目的 DLL 文件中。当您更改代码隐藏并且不构建项目时,您基本上有该页面的两个版本,而 ASP.Net 无法解析这两个版本。因此,当您编译该类时,更改会被整合到 DLL 中。

回答by Balaji Birajdar

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

尤里卡啊!!明白了...这个有效...

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

当您拖放用户控件或通过该LoadControl方法加载它时,请在其路径中使用“波浪号”。例如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.

这同样适用于放置在页面上的控件。将波浪号添加到 aspx 页面中用户控件的声明路径。

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

我们可以将其称为 VS2005 中的错误,并在 VS 2008 中修复。

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

如果这不起作用或需要更多解释,请回复我。或者,不要忘记将此标记为答案。