C# “无法加载类型 [Namespace].Global”让我感到悲伤
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2005747/
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
"Could not load type [Namespace].Global" causing me grief
提问by gkdm
In my .Net 2.0 Asp.net WebForms app, I have my Global.asax containing the following code:
在我的 .Net 2.0 Asp.net WebForms 应用程序中,我的 Global.asax 包含以下代码:
<%@ Application CodeBehind="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %>
However when I build I get an error stating-
但是,当我构建时,我收到一个错误说明-
Could not load type 'MyNamespace.Global'.
无法加载类型“MyNamespace.Global”。
This seems to be because the MyNamespace namespace (defined in the code behind file Global.asax.cs) is not seen by the compiler in the Global.asax file (does not show in R# intellisence..). This turned out to be a very hard nut to crack... any help will be appreciated!
这似乎是因为 MyNamespace 命名空间(在文件 Global.asax.cs 后面的代码中定义)在 Global.asax 文件中没有被编译器看到(在 R# 智能中没有显示..)。结果证明这是一个非常难以破解的难题……任何帮助将不胜感激!
Note: The Global.asax and the Global.asax.cs are located in the same folder.
注意:Global.asax 和 Global.asax.cs 位于同一文件夹中。
Note2: When compiling from the vs prompt with csc it compiles o.k.
注意2:当从 vs 提示符下用 csc 编译时,它编译正常
采纳答案by stantona
One situation I've encountered which caused this problem is when you specify the platform for a build through "Build Configuration".
我遇到的导致此问题的一种情况是,当您通过“构建配置”指定构建平台时。
If you specify x86 as your build platform, visual studio will automatically assign bin/x86/Debug as your output directory for this project. This is perfectly valid for other project types, except for web applications where ASP.NET expects the assemblies to be output to the Bin folder.
如果你指定 x86 作为你的构建平台,visual studio 会自动分配 bin/x86/Debug 作为你这个项目的输出目录。这对于其他项目类型完全有效,但 ASP.NET 期望将程序集输出到 Bin 文件夹的 Web 应用程序除外。
What I found in my situation was that they were being output to both (Bin and Bin/x86/Debug), with the exception that some of the dll's, and inexplicably the most important one being your web application dll, being missing from the Bin folder.
我在我的情况下发现它们被同时输出到(Bin 和 Bin/x86/Debug),除了一些 dll,莫名其妙地最重要的一个是你的 web 应用程序 dll,从 Bin 中丢失文件夹。
This obviously caused a compilation problem and hence the "Could not load type Global" exception. Cleaning the solution and deleting the assemblies made no difference to subsequent builds. My solution was to just change the output path in project settings for the web app to Bin (rather than bin/x86/Debug).
这显然导致了编译问题,因此出现了“无法加载全局类型”异常。清理解决方案和删除程序集对后续构建没有影响。我的解决方案是将 Web 应用程序的项目设置中的输出路径更改为 Bin(而不是 bin/x86/Debug)。
回答by PhilPursglove
Have you changed the namespace of your project? I've seen this happen occasionally where I've changed the namespace in the Project Properties dialog but Visual Studio hasn't changed the namespace
declaration in existing code files.
您是否更改了项目的命名空间?我在“项目属性”对话框中更改了命名空间但 Visual Studio 没有更改namespace
现有代码文件中的声明时,偶尔会发生这种情况。
回答by jyoungdev
Check the Build Action of Global.asax.cs. It should be set to Compile.
检查 Global.asax.cs 的构建操作。它应该设置为编译。
In Solution Explorer, Right-click Global.asax.cs and go to Properties. In the Properties pane, set the Build Action (while notdebugging).
在解决方案资源管理器中,右键单击 Global.asax.cs 并转到属性。在“属性”窗格中,设置“构建操作”(不调试时)。
It seems that VS 2008 does not always add the .asax(.cs) files correctly by default.
默认情况下,VS 2008 似乎并不总是正确添加 .asax(.cs) 文件。
回答by sri
I am new to asp .net developement and I faced the similar problem.
我是 .net 开发的新手,我遇到了类似的问题。
I updated the class as a partial
class and it worked fine.
我将课程更新为一个partial
课程,并且效果很好。
public partial class Global : System.Web.HttpApplication
回答by Sergey
In my situation it was related to Web Site/Web Application type of the project. We recently moved to MVC and had to change it to Web Application.
在我的情况下,它与项目的网站/Web 应用程序类型有关。我们最近转移到 MVC,不得不将其更改为 Web 应用程序。
So, the solution was simple: select your web-site in Solution Explorer and remove it from the solution, then right click on the solution and select Add -> Existing Project (not Web Site), recompile the web-site.
因此,解决方案很简单:在解决方案资源管理器中选择您的网站并将其从解决方案中删除,然后右键单击解决方案并选择添加 -> 现有项目(不是网站),重新编译网站。
回答by archangel76
If you rebuild or change a project and move over files from an old one, make sure to check the Inherit block of your global. In my case, the former project/solution was named intranet, and I had recreated it as Intranet, but when I moved over the files, it didn't like the lowercase (duh). Just do a general look-through of the names of the files.
如果您重建或更改项目并从旧项目移动文件,请确保检查全局的 Inherit 块。在我的例子中,以前的项目/解决方案被命名为 Intranet,我将它重新创建为 Intranet,但是当我移动文件时,它不喜欢小写(废话)。只需对文件名称进行一般浏览即可。
回答by rageit
I was befuddled by the same darn issue. I tried to remove and and the global.asax
(closed VS2010 before adding). Cleaned the project/solution, checked for any changes in the web application configuration and other stuffs that had worked for other people here in SO threads. I finally cleaned the solution, deleted the bin/obj folders and stopped any running VS2010 development servers then I reverted all the changes back and found the application was running again. I redid the same things and now its working fine.
我被同样的该死的问题弄糊涂了。我试图删除和global.asax
(在添加之前关闭VS2010)。清理项目/解决方案,检查 Web 应用程序配置中的任何更改以及在 SO 线程中为其他人工作的其他内容。我终于清理了解决方案,删除了 bin/obj 文件夹并停止了任何正在运行的 VS2010 开发服务器,然后我恢复了所有更改,发现应用程序再次运行。我重做了同样的事情,现在它工作正常。
Happened again and this time thissolution worked for me.
再次发生,这次这个解决方案对我有用。
回答by mjroodt
Old post but I go this error when trying to convert from website project to web application project.
旧帖子,但在尝试从网站项目转换为 Web 应用程序项目时出现此错误。
Follow the instructions on this Link. I still got the global.asax error but all I did was delete it and re-add it back by right clicking on the project in visual studio and selecting add new item. Add the global.asax file and it worked.
按照此链接上的说明进行操作。我仍然收到 global.asax 错误,但我所做的只是删除它,然后通过右键单击 Visual Studio 中的项目并选择添加新项目来重新添加它。添加 global.asax 文件,它起作用了。
回答by jasonjonesutah
I experienced a similar error when having a
我遇到了类似的错误
<clear/>
tag as a child (the first child) of the
标记为孩子(第一个孩子)
<assemblies>
tag in my Web.config. I had inserted the tags in my web.config in an attempt to prevent configuration inheritance in an application deployed under the 'Default Web Site' in IIS.
在我的Web.config 中标记。我在我的 web.config 中插入了标签,以防止在 IIS 的“默认网站”下部署的应用程序中的配置继承。
回答by panky sharma
I had a similar issues where I was receiving this error on a project.
我有一个类似的问题,我在一个项目中收到这个错误。
“Could not load type [Namespace].Global
Error in Line 1 etc etc
After spending some time I suspect a function with possible errors in a Class ..later commenting that specific function my problem get resolved.
花了一些时间后,我怀疑类中可能存在错误的函数..后来评论该特定函数我的问题得到解决。
I dont know why Visual Studio did't give me that specific error at debugging time. But this error might occure due to some errors in class file..
我不知道为什么 Visual Studio 在调试时没有给我那个特定的错误。但是这个错误可能是由于类文件中的一些错误而发生的。