C# 解析器错误消息:无法加载类型“sometype”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15071220/
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-08-10 13:58:14  来源:igfitidea点击:

Parser Error Message: Could not load type 'sometype'

c#asp.netglobal-asax

提问by AnchovyLegend

I am experiencing an error that I am unable to resolve for some time now. I was wondering if someone can help identify the cause of this error? I am completely new to asp / asax. After some research, I think that the error I am getting is due to the web application trying to use outdated code. I was thinking to rebuild the c# file using Visual Studio and/or the entire project. However, I am completely new to C# and asp, and was wondering can give me some suggestions if this may fix the problem and/or if there is an possible alternate solution.

我遇到了一个我现在有一段时间无法解决的错误。我想知道是否有人可以帮助确定此错误的原因?我对asp/asax完全陌生。经过一番研究,我认为我得到的错误是由于 Web 应用程序试图使用过时的代码。我正在考虑使用 Visual Studio 和/或整个项目重建 c# 文件。但是,我对 C# 和 asp 完全陌生,并且想知道这是否可以解决问题和/或是否有可能的替代解决方案给我一些建议。

Error message

错误信息

Parser Error Message: Could not load type 'Inventory1.Global'.

Source Error:   <%@ Application Codebehind="Global.asax.cs" Inherits="Inventory1.Global" %>

EntireGlobal.asax contents:

整个Global.asax 内容:

<%@ Application Codebehind="Global.asax.cs" Inherits="Inventory1.Global" %>

采纳答案by Taha Rehman Siddiqui

Try replacing CodeBehindwith CodeFile

尝试更换代码隐藏的CodeFile

回答by DiskJunky

It's likely that you renamed something. Check the Global.asax.cs file for the class declaration and make sure that the namespace and class name match exactlywhat's in the asax file. This includes case! Can you copy/paste the namespace and class declaration of the .cs file into a post here so that we can compare?

你很可能重命名了一些东西。检查 Global.asax.cs 文件中的类声明,并确保命名空间和类名与asax 文件中的内容完全匹配。这包括案例!您能否将 .cs 文件的命名空间和类声明复制/粘贴到此处的帖子中以便我们进行比较?

回答by 3Dave

Could not load type

无法加载类型

means that a type could not be loaded. (In this case, "type" refers to Inventory1.Global). Types are located in compiled DLLs. So, either the DLL isn't available, is out of date, or doesn't contain a publictype with the given name.

意味着无法加载类型。(在这种情况下,“类型”是指Inventory1.Global)。类型位于已编译的 DLL 中。因此,DLL 不可用、已过期或不包含public具有给定名称的类型。

Some possible causes are:

一些可能的原因是:

  • You have no type declared with the given name. For your example, you should have the following:
  • 您没有使用给定名称声明的类型。对于您的示例,您应该具有以下内容:
namespace Inventory1 {
  public class Global {
  ...
  }
}
namespace Inventory1 {
  public class Global {
  ...
  }
}

Note: avoid names like Inventory1. They imply that there is an Inventory2, Inventory3, etc., which is bad practice as they're abmiguous and not very descriptive. Also, Globalis pretty vague, and may introduce confusion with the global namespace.

注意:避免使用像Inventory1. 他们暗示存在Inventory2Inventory3等等,这是不好的做法,因为他们是abmiguous,不是很描述。此外,Global非常模糊,可能会与全局命名空间混淆。

  • Make sure your cases match (Inventory1, not INVENTORY1.)
  • You haven't compiled the project. In VS, rebuild the solution.
  • The assembly that declares the class has a compilation error, so the relevant DLL is either missing or out of date. Make sure you've resolved all errors.
  • The class is not marked as public.
  • 确保您的案例匹配 ( Inventory1, 不是INVENTORY1.)
  • 你还没有编译项目。在 VS 中,重建解决方案。
  • 声明该类的程序集存在编译错误,因此相关的 DLL 要么丢失,要么已过期。确保您已解决所有错误。
  • 该类未标记为public

If I had to guess, I'd put my money on a compilation error. Unlike PHP and other interpreted languages, C# have to be successfully compiled before they can be used.

如果我不得不猜测,我会把钱花在编译错误上。与 PHP 和其他解释型语言不同,C# 必须成功编译才能使用。

回答by Neil

Parser Error Message: Could not load type __

解析器错误消息:无法加载类型__

After doing everything suggested in the comments above, with no luck, refreshing (uploading) the contents of /bin to the server worked. The files uploaded to bin are the: dll, pdb and xml. Don't know which one did it.

在完成上述评论中建议的所有操作后,运气不佳,将 /bin 的内容刷新(上传)到服务器工作正常。上传到 bin 的文件有:dll、pdb 和 xml。不知道是谁做的。

The problem I had here was induced by renaming a file (_.aspx) in Solution Explorer.

我在这里遇到的问题是通过在解决方案资源管理器中重命名文件 ( _.aspx)引起的。

回答by Vignesh Subramanian

I faced this issue and i got the solution from hereand i would like to share it.

我遇到了这个问题,我从这里得到了解决方案,我想分享一下。

SOLUTIONEmpty the bin folder. Build all the dependent class libraries and refer them in the main project and build the complete solution.

解决方法清空 bin 文件夹。构建所有依赖类库并在主项目中引用它们并构建完整的解决方案。

I did this and it worked like a charm for me !!

我这样做了,它对我来说就像一个魅力!

回答by Vignesh Subramanian

I had this error , just needed to rebuild the project

我有这个错误,只需要重建项目

回答by zwooter

This happened with me on my local machine. The issue was incorrect IISExpres config. If you are getting this issue on your local environment (Visual Studio debug runs), check the IIS Express config file. Make sure your local site/application path is pointing to the correct location.

这发生在我的本地机器上。问题是 IISExpres 配置不正确。如果您在本地环境中遇到此问题(Visual Studio 调试运行),请检查 IIS Express 配置文件。确保您的本地站点/应用程序路径指向正确的位置。

The configuration file is called applicationhost.config. It's stored here: My Documents > IIS Express > config . Usually (not always) one of these paths will work:

配置文件名为applicationhost.config. 它存储在这里:我的文档 > IIS Express > config 。通常(并非总是)这些路径之一会起作用:

  • %userprofile%\documents\iisexpress\config\applicationhost.config
  • %userprofile%\my documents\iisexpress\config\applicationhost.config
  • %userprofile%\documents\iisexpress\config\applicationhost.config
  • %userprofile%\我的文档\iisexpress\config\applicationhost.config

回答by vapcguy

Rebuilding/re-publishing my project/solution to the server did nothing to help me, and I doubt that will help that many out of this predicament. For me, I did a few things to troubleshoot this that eventually got me out of this "hole".

重建/重新发布我的项目/解决方案到服务器对我没有任何帮助,我怀疑这是否会帮助很多人摆脱这种困境。对我来说,我做了一些事情来解决这个问题,最终让我摆脱了这个“漏洞”。

I had been trying to use a binding on the web site, but this wasn't working. I tried calling the site with http://localhost/Report.aspx(this was my homepage, which I opted to not call Default.aspx- I was going to update the "Default Documents" section with the name later) when I got the Parser Error the OP saw. So I tried some things:

我一直试图在网站上使用绑定,但这不起作用。当我收到 OP 看到的解析器错误时http://localhost/Report.aspx,我尝试使用该网站(这是我的主页,我选择不调用它Default.aspx- 我稍后将使用该名称更新“默认文档”部分)。所以我尝试了一些事情:

I stopped the old project's website and built another, simple web project, that had "hello" and a label on the page and nothing else. I had a line in the Page_Load to populate the label's Text property with "world!", just to make sure that part was working. I created a new website on port 80 and transferred the published contents of my site to the server. So even though I had .NET 4.5 installed on the server (and had ran the aspnet_regiis -i command from the 4.0 directory) and the App Pool in IIS that I was using for this new project was set to 4.0, the browser complained about the web.config having a targetFramework=4.5.2in it, which is Visual Studio 2015's default framework. So I installed .NET 4.6 (NDP46-KB3045557-x86-x64-AllOS-ENU.exe), restarted the server, and then my simple site worked. So then I deleted this site - all I wanted to do was prove my installation steps were accurate and the server could run a site.

我停止了旧项目的网站,并构建了另一个简单的 Web 项目,它在页面上有“你好”和一个标签,没有别的。我在 Page_Load 中有一行用“world!”填充标签的 Text 属性,以确保该部分正常工作。我在端口 80 上创建了一个新网站,并将我网站的已发布内容传输到服务器。因此,即使我在服务器上安装了 .NET 4.5(并从 4.0 目录运行了 aspnet_regiis -i 命令)并且我用于这个新项目的 IIS 中的应用程序池设置为 4.0,浏览器仍抱怨web.config 有一个targetFramework=4.5.2其中,这是 Visual Studio 2015 的默认框架。所以我安装了 .NET 4.6 (NDP46-KB3045557-x86-x64-AllOS-ENU.exe),重新启动了服务器,然后我的简单站点就可以工作了。然后我删除了这个站点——我想做的就是证明我的安装步骤是准确的,并且服务器可以运行一个站点。

So then I went back to my original project/site - I deleted and re-created the web site. I put the Application Pool to the one I had originally created for this, which I ensured was running .NET 4.0. Once I did this, I navigated to my site and everything worked when using http://localhost/Report.aspx. So it seems to me what causes this is what version of the .NET Framework you are using.

然后我回到我原来的项目/站点 - 我删除并重新创建了该网站。我将应用程序池放在我最初为此创建的应用程序池中,我确保它运行的是 .NET 4.0。完成此操作后,我导航到我的网站,使用http://localhost/Report.aspx. 所以在我看来是什么导致了这是你使用的 .NET Framework 的版本。

回答by SteveFerg

I tried all the solutions listed above and none of them worked. I finally created a new web page (webform) and copy blocked all the code (cs and aspx files) into it from the old one, deleted the old cs and aspx file, recompiled, and now I'm back in business. I know it makes no sense. It should not have mattered, but it worked.

我尝试了上面列出的所有解决方案,但都没有奏效。我终于创建了一个新的网页(webform)并将所有代码(cs 和 aspx 文件)从旧的复制到其中,删除了旧的 cs 和 aspx 文件,重新编译,现在我又回来了。我知道这毫无意义。它应该无关紧要,但它起作用了。

回答by ankita shrivastava

Please try to open your project as Project/Solution, most probably it will resolve the error. This type of error Could not load type....occurs when we try to open project as website.

请尝试将您的项目作为项目/解决方案打开,很可能它会解决错误。当我们尝试将项目作为网站打开时,会出现此类错误无法加载类型...。

I have tried to open my project as solution and it resolved my problem.

我试图打开我的项目作为解决方案,它解决了我的问题。