0

I am working on an ASP.NET website and as part of the website I created a file called Global.cs which is used to hold all my helper classes. However, unfortunately, I am constantly getting errors saying there has been a problem while compiling this file. All the classes inside the file are included in single namespace - "Global".

When using it 'inline' eg. Global.SomeStaticClass.Method() :

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'Global' does not exist in the current context

When using "using Global;" directive:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'Global' could not be found (are you missing a using directive or an assembly reference?)

As suggested in many posts, I tried including the class in App_Code folder of my project and the namespace, "Global", is detected by the Visual Studio (no errors when adding "using Global;" directive, or when using it inline "Global.Class.Method()". However, it still fails whenever I try to access the page in the browser.

Community
  • 1
  • 1
zryw141
  • 81
  • 6
  • Is the Build Action on your file correctly set to "Compile"? – Chris Schmitz Aug 02 '16 at 12:05
  • I checked and there is no Build Action property for this file. – zryw141 Aug 02 '16 at 12:08
  • There should be. Maybe you should try to add a new source code file to your project and copy the content over to the new file. A general advice: If it isn't needed for any specific reason, it is generally considered bad practice to place multiple classes in a single file with a single namespace. Why are you doing it that way? – Chris Schmitz Aug 02 '16 at 12:18
  • I think you can set the Build Action in Wep App projects only and not in Website projects. As to including the multiple classes in single file, I have split it as you suggested. – zryw141 Aug 02 '16 at 12:24
  • Sadly i have no clue about Website projects. Maybe you should see this post, and decide if there is a way for you to use a Web Application project. (http://stackoverflow.com/questions/398037/asp-net-web-site-or-asp-net-web-application) The author of the accepted answer also points out, there might be problems with uncompiled classes that are being referenced somewhere else. If you don't have this possibility, you should make yourself clear how the compilation/deployment process is working, because it looks like your file isn't compiled or deployed correctly. Sadly i can't help you with that. – Chris Schmitz Aug 02 '16 at 12:53

1 Answers1

0

I could not find a simple solution to my problem and decided that the easiest and the most time efficient solution will be to convert my Website project to a Web Application project. You can read how to convert your Website to a Web Application at: https://blogs.msdn.microsoft.com/webdev/2009/10/29/converting-a-web-site-project-to-a-web-application-project/

With the project converted to a Web App I was able to attach my class files after compiling them to a DLL library, as it did not accept any uncompiled files inside App_Code folder by throwing an exception.

The directory '/website/App_Code/' is not allowed because the application is precompiled.

If you encounter the same issue, you can learn how to compile C# files using Developer Command Prompt here: https://msdn.microsoft.com/en-us/library/78f4aasd.aspx

zryw141
  • 81
  • 6