“无法加载类型[命名空间]。全局”让我很伤心

在我的.Net 2.0 Asp.Net WebForms 应用程序中,Global.asax 包含以下代码:

<%@ Application CodeBehind="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %>

但是,当我建立我得到一个错误说-

无法加载类型“ 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..). 事实证明这是一个很难解决的难题... 任何帮助都会受到感激!

注意: Global.asax 和 Global.asax.cs 位于同一个文件夹中。

注2: 当使用 csc 从 vs 提示符进行编译时,它会编译 ok。

141332 次浏览

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.

Check the Build Action of Global.asax.cs. It should be set to Compile.

In Solution Explorer, Right-click Global.asax.cs and go to Properties. In the Properties pane, set the Build Action (while not debugging).

It seems that VS 2008 does not always add the .asax(.cs) files correctly by default.

I am new to asp .net developement and I faced the similar problem.

I updated the class as a partial class and it worked fine.

public partial class Global : System.Web.HttpApplication

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.

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.

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).

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.

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.

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.

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.

Happened again and this time this solution worked for me.

Old post but I go this error when trying to convert from website project to web application project.

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.

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.

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..

go to configuration Manager under properties for your solution. Then make sure all projects and getting built, and this won't be a problem.

Change the GUID of the assembly. This fixes many MANY problems, I have found.

None of these worked for me, unfortunately. The fix I found was more specific to development, specifically, debugging on your local machine. Also unfortunately, it doesn't really fix the issue in the way I was hoping, but if you're at your wit's end, this might get you running again.

TL;DR: In the project properties, in the web tab, under Servers, Select Use Local IIS Web Server. The address,

http://localhost/MyApp"

was already populated (I have IIS7, .NET 4.0). What was originally selected was the "Use Visual Studio Development Server" with a Virtual path of "/"

What's really puzzling to me is the fact that nothing else worked, I went through all of the suggestions I could find on SO, and nothing would work. The weird thing is, the error was (seemingly, it's been a few months since I last looked) manifested when I added a new ascx file that was similar to an existing one that was added from an old .net 2.0 (I think) project that would allow for custom user management from within the application. It was working, beautifully, for a long time, until I tried adding this new file. After adding it, and seeing the error, I promptly reverted all changes, but the Global.ascx error would not go away, not even blowing away the whole project and re-getting latest from source control.

This work for me: First thing: it's seem that no matter what did you say to visual studio, the ide always look the file in: bin (for web app and off course in my case) So, even when i said to visual studio a specific path to load file, the ide keep looking for the wrong path. So i change in the: Build/Configuration Manager the output type to: Release (previous i clean up the solution, even manually) so, when the file .dll was created then i moved manually to "bin" folder under the project/solution folder. Hope this will be helpfull !!

Here's another one for the books. It appears that this happens when you launch more than one web application from the same port number.

Basically, I have a couple of branches that I work off, I have a main branch and a staging branch and a release branch. When I switched branch to the staging branch I noticed that it was using the same port address configuration so I opted to change that. I then received another warning that this reservation conflicts with another configured application. The IIS Express server is sensitive about that and for whatever reason it bricks the configuration.

By simply picking a third unaffected port this problem went away because it then maps the port to a new directory mapping (my branches are located differently on disk). I noticed this because I tried to change the type name pointed to by Global.asax but the type name was unchanged even after restarting the server, so clearly, the code I was changing wasn't reflected by the IIS Express deployment.

So, before you lose too much sleep over this, try changing the IIS port number that is currently used to run the web project.

I had converted my solution from VS2003 to VS2010 and had had problems converting the web application project.

I experienced the exact same problem and none of the answers worked for me.

What worked for me was:

  • Right Click on the solution and select Configuration Manager
  • Looked at each of the configurations in the 'Active solution configuration' drop down list.
  • Included the web application in the build by ticking the 'Build' check-box.

it would seem that the problems I had during the conversion had removed the web application project from the build for some reason.

Hopefully this answer helps anyone else that has the same problem...

I restarted Visual Studio and error was gone!

When I encountered this problem most recently I tried everything mentioned here but to no avail. After tearing my hair out I decided to try deleting my entire code base (yes, pretty desperate!) and then re-downloaded everything from my code repository. After doing this everything worked fine once more.

Seems an extreme solution but just thought I'd include it here as it hasn't been mentioned before in this thread.

(Note that the other time I have encountered this issue, is when the Global.asax was inheriting from a component which needed to be registered on the host machine. This was missing hence I got this same issue).

TL;DR; If all answers in this thread don't work for you, try deleting and then re-downloading your entire code base!

In my case, It was because of my target processor (x64) I changed it to x86 cleaned the project, restarted VS(2012) and rebuilt the project; then it was gone.

I've run across this issue a few times and in each case I was rebuilding a computer or switching to a new computer. My first step (besides updating the machine and installing Visual Studio) is to pull my projects down from Git and test them.

I hit this error each time because I tried to access my local code before compiling it. You see, I have Git and Subversion setup to ignore my bin/build folders, so after a pull from my repository I forgot to run a build which pulls required packages from Nuget (since I have Git/SVN ignore those too) and creates the DLLs needed to actually run my app.

I doubt this will resolve most people's issues, but I didn't see it on the list of potential solutions so I thought I'd add it.

I had to delete (duplicate) files from disk that were not included in the project. Looks like the duplicates were caused by a failed rename. The file names were different, but same code.

After deleting all the oof.* files I was able to scan.

  • foo.aspx
  • foo.aspx.cs
  • foo.aspx.designer.cs
  • oof.aspx
  • oof.aspx.cs
  • oof.aspx.designer.cs

in my case it was IISExpress pointing to the same port as IIS to solve it go to

C:\Users\Your-User-Name\Documents\IISExpress\config\applicationhost.config

and search for the port, you will find <site>...</site> tag that you need to remove or comment it

In my case, I had added Global.asax to a WCF project to experiment with it, but decided to remove it. I removed it from Solution Explorer, but because it was still in the folder, the pipeline was still finding it and causing this error.

I removed the Global.ASAX and GLobal.asax.cs from the filesystem and that did resolve the error.

  1. Right Click on Project Solution and Select Batch Build.
  2. Then select Your Project Name and Clean And Rebuild.

Works Fine For Me IN VS 2015.Now I can Use Global event. My Global.asax file has this Line

<%@ Application Language="C#" CodeBehind="~/App_Code/Global.asax.cs" Inherits="Global" %>

And I make class file Global.asax.cs that is in AppCode folder that look like

public partial class Global : HttpApplication
{
public Global()
{
//
// TODO: Add constructor logic here
//
}
}

I hope this will help

I had this issue when deploying on prod server only. In my other environments it works... I just deleted stuff in the bin folder then republish and it work after that.

If you are using Visual Studio, You probably are trying to execute the application in the Release Mode, try changing it to the Debug Mode.

I've tried to rebuild solution and clear ASP.NET Temporary files without success.
But after run IISRESET the error disappeared.

Update: I had the same problem again 1 month later. I've noticed that MyWebsite.DLL exists in bin folder, but doesn't exist in Temporary ASP.NET Files (C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files). I've tried a few things, that are suggested on this and "Parser Error Message: Could not load type" in Global.asax questions (I actually forgot about my own answer), but the error disappeared again only after IISRESET

Well In my case VS 2017, the lightweight solution load was causing this issue. I disabled it & restarted VS then re-build my solution & the problem gone.

Just wanted to add my two cents. I was receiving the same error, and I tried all the suggestions to no avail. My situation is probably different?

Turns out, an auto-generated "AssemblyInfo.cs" file had some extraneous spaces, which was preventing me from launching the web app (via debug). Here's what the file looked like:

[assembly: AssemblyTitle("WebApplication2")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("


")]
[assembly: AssemblyProduct("WebApplication2")]
[assembly: AssemblyCopyright("Copyright ©


2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

After killing the spaces in the AssemblyCompany and AssemblyCopyright, I was finally able to build and launch the project.

Observed in the following environment: --Visual Studio 2017 Community version 15.3.0 --Win 7 x64 Enterprise --New Project > Visual C# > Web > ASP.NET Web Application > Web Forms

I experienced this problem when I accidentally set "Chrome" to be the default browser for debugging. When I set it back to "IE" the problem disappeared. I am not sure why...


EDIT: I was about to delete this answer, because I wasn't sure about it, but then I had the problem again. I switched to browsing with Chrome, then back again to IE and it stopped! What gives!?

As silly as it may sound. Here is what I did..

My project was targeting 4.6.1 version. Changed the target version to 4.6.2. built it. deleted obj & bin folders. & once again changed the version to 4.6.1. Bingo!!

Having been in the development game for almost 20 years, this chestnut has continued to plague me across multiple projects.

As such, today whilst experiencing this same problem again, against another project, I decided to investigate further and I do believe this is related to Bin folder location... or more specifically, the output path of the bin folder.

For me, for a simple service based web application configured to run/debug through IIS, it was changing the output path from bin\debug to bin\ that resolved it

Project > Properties > Build > Output path

Seriously hopes this helps.

You need to set your Output path to bin\

We have a .net app running on a PROD server. When we copied the bin folder to the server for the latest release, we got the same error. We couldn't understand what was going on. It worked fine locally and on the TEST server. After some digging I noticed that some of the uploaded files had a size of 0 bytes.

It turned out it was the FTP client (Filezilla) that corrupted some of the uploaded files, which understandably made .net think some libraries was located elsewhere or missing.

We tried another FTP client and re-uploaded all files. And that seemed to have done the trick.