13

I'm currently working on an ASP.NET 4.0 site using a project-less solution.

By default the global.asax does not have a code-behind file, but after I changed it to

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

And created an empty Global.asax.cs with the same namespace / class name I'm getting this error at compile time

Error 1 Could not load type 'MyNamespace.Global'. C:\Projects\RiskOptix\Code\RiskOptix.WebApplication\RiskOptix.WebApp\Global.asax 1

I've already tried cleaning out my entire bin folder but to no avail - this is extremely infuriating.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
manning18
  • 957
  • 3
  • 8
  • 18

4 Answers4

22

This question has already been asked. Check out this answer. Web site projects work differently from web application projects. Website type projects do not have CodeBehind files instead have CodeFile.

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

CodeBehind = Needs to be compiled ( asp.net 1.1 model) and compiled binary is placed in the bin folder of the website. You need to do a compile in visual studio before you deploy. Good model when you do not want the source code to be viewable as plain text ... for example when delivering to a customer who you not have obligation to provide code.

CodeFile = You provide the source file with the solution for deployment. ASP.NET 2.0 runtime compiles the code when needed. The compiled files are at Microsfot.NET[.NET version]\Temporary ASP.NET Files.

Community
  • 1
  • 1
coder net
  • 3,447
  • 5
  • 31
  • 40
  • That worked - but I don't understand why it worked. I had a bare bones class declared (inherited from HttpApplication but no Application_Start method) – manning18 Aug 17 '11 at 17:01
  • I still have another problem - the MyNamespace.Global type is still not visible to other .cs files within the website, despite using the appropriate namespace, although now at least it compiles and runs. I need to be able to reference a static property in the Global.asax – manning18 Aug 17 '11 at 17:15
  • The web site projects have a different type of "CodeBehind" called "CodeFile". check out http://stackoverflow.com/questions/73022/codefile-vs-codebehind . Is there any reason why you have to use a "web site" type project. just convert it to "web application" or create a new "web app" project if you have no specific needs. – coder net Aug 17 '11 at 17:23
  • I found an alternative solution (that fixed my issue with being unable to reference the Global class from other files) I copied the Global.asax.cs into the App_Code folder and omitted the CodeFile attribute and retained the Inherits attribute. By doing so, the Global.asax.cs is compiled into the assembly and is now visible to my other .cs files. – manning18 Aug 17 '11 at 17:28
  • coder net: thank you so much for that link, that clarified things IMMENSELY and gave me a much greater insight into what's actually going on behind the scenes. I never noticed that those were separate attributes to be honest (guess it's because they both start with "Code" lol) – manning18 Aug 17 '11 at 17:29
6

I was not able to run my MVC Web application. It was giving Could not load type <application namespace.Classname> error in Global.asax

I went to Project Properties and set the build>output folder to bin/ which was bin/Debug. Ran it once. It ran fine. And then again set the output folder to bin/Debug. Working fine now.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
Manik Mittal
  • 269
  • 1
  • 5
  • 10
  • You need to keep it building to `/bin` or the MVC web app will continue to get out of sync with the source. Your best bet is changing all build configurations (*All Configurations*) to use `/bin` as the output path and this won't happen again. – SliverNinja - MSFT Nov 16 '12 at 20:42
1

just to add my 2 cents with a WTF moment, My version of this error was caused by the Global.asax.cs not being included in the Visual Studio Project.

Right clicked on the .cs file, include in project and voila...

HTH

Dave

Dave
  • 740
  • 1
  • 6
  • 17
0

Yet another way to get this into this problem...

I had my web app open in VS2010, and IIS Express could run it just fine. Later, I opened the same web app, but in a newer branch with VS2012, and the virtual dirs in IIS Express auto-magically updated themselves to the physical dirs for the VS2012 project, without warning. So when I hit F5 to run my web app in VS2010 where I'm debugging, then I got the "could not load type or namespace" error in the global.asax file on one Import Namespace line. Closing both VS instances and reopening just the VS2010 version fixed the problem.

Chris O
  • 5,017
  • 3
  • 35
  • 42