53

I created a RazorFunctions.cshtml file on App_Code

@functions {
    public static string GetActiveClassIf(string controllerName, string actionName = null)
    {
        var routeData = @HttpContext.Current.Request.RequestContext.RouteData;
        string currentController = routeData.Values["controller"].ToString();
        string currentAction = routeData.Values["action"].ToString();
        return controllerName == currentController &&
            (String.IsNullOrEmpty(actionName) || currentAction == actionName) ? "active" : "";
    }
}

and when I compile, it give me 2 errors (compilation get success and site work without problem) but the errors are annoying.

The RazorFunctions.cshtml are as Content (tried compile but doesn't work with cshtml files of course)

Global.asax.cs is :

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        ModelMetadataConfig.RegisterModelMetadata();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
        //Database.SetInitializer(new DropCreateDatabaseAlways<ApplicationDbContext>());
    }
}

and

<%@ Application Codebehind="Global.asax.cs" Inherits="Bouron.Web.MvcApplication" Language="C#" %>

This is the first time I use the App_Code so I don't know what else to do, all search returns ASP.NET 1-2 results, are out of date where razor doesn't even exist so I'm not sure how can I solve this.

I'm using Visual Studio 2015 (just in case it matters)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bart Calixto
  • 19,210
  • 11
  • 78
  • 114
  • 2
    Did you find a solution? Got the same problem. – Andreas Furster May 20 '15 at 11:17
  • 5
    I had the same issue, manually edited the generated file `file.cshtml.72cecc2a.cs` and changed `ASP.global_asax` to `System.Web.HttpApplication` and the error went away... don't know why it is happening though – Orlando Osorio May 26 '15 at 15:15
  • @OrlandoWilliam can you post that as an answer. because indeed that also fixes the issue for me so I can accept it. Thanks – Bart Calixto May 26 '15 at 20:29
  • I've given up on trying to prevent the "error" from appearing. I wonder if there's any way to suppress CS0234? – Rudey Oct 07 '18 at 14:09

10 Answers10

47

After further investigation I found that this is a known issue.

source: https://support.microsoft.com/en-us/kb/3025133

ASP.NET and Web Development

  • When you create a Web Forms 4.5 WAP and open a Web Form page, you receive the following errors in the Errors List window:

  • The project will run without any issues. Error CS0234 The type or namespace name 'global_asax' does not exist in the namespace 'ASP' (are you missing an assembly reference?) Error CS0234 The type or namespace name 'Linq' does not exist in the namespace 'System' (are you missing an assembly reference?) Assume that you use new language features for C# and VB in Visual Studio 2015 RC. You receive a runtime error when you use C# or VB in a Web Form page or in Razor Views.

To work around this issue, install Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package. This package will substitute the Roslyn-based provider for the in-box CodeDom providers for ASP.NET.

Bart Calixto
  • 19,210
  • 11
  • 78
  • 114
  • 7
    For whatever reason I had to also open the file that it was throwing an error in Visual Studio to make it stop showing up in the error list. It's like the intellisense wasn't reconsidering the file until I opened it. – adotout Jul 22 '15 at 20:03
  • 10
    I needed to restart VS2015 RTM after installing the package above to get this fixed. – Stafford Williams Aug 18 '15 at 14:15
  • 2
    This worked for me. But after installing DotNetCompilerPlatform I started receving NuGet error about an invalid Path. If you ran into the same problem here is the solution for that: http://stackoverflow.com/a/31826795/573208 – To Ka Aug 19 '15 at 07:15
  • 1
    @adotout also may help manually delete temp files from the Temporary ASP.NET Files folder. And if you open this project in old visual studio(before 2015) you may need to repeat these steps. And after installing Microsoft.CodeDom.Providers.DotNetCompilerPlatform and running project in vs2015 you may have troubles with compilation if you have problem with Encoding in your code files. You need to set same encoding for all code files(for example Utf-8) if it's not. – chromigo Aug 23 '15 at 18:35
  • 1
    I ended up upgrading my project from .NET 4.5.1 to .NET 4.6 and the error finally disappeared. – Phil Sep 14 '15 at 15:15
  • 1
    I take back what I said... I still get this problem and nothing seems to work until I open the helper files as described in the current open [connect issue](https://connect.microsoft.com/VisualStudio/feedback/details/1304446/global-razor-helpers-generate-cs0234-the-type-or-namespace-name-global-asax-does-not-exist-error-in-ide) – Phil Sep 14 '15 at 15:29
  • 3
    This didn't work for me - I still have to have the helper file open, or I get the error, even after installing the recommended nuget package. Quite annoying. – Ken Smith Feb 22 '16 at 22:43
  • 10
    The source cited in the answer was for Visual Studio 2015 **RC**. I am using VS 2015 **Update 1** and seeing the `global_asax` error but not the `Linq` error. Regrettably, the NuGet package does not get rid of the `global_asax` error. – Mike Grove aka Theophilus Feb 29 '16 at 17:05
25

I had the same issue.

I manually edited the generated file file.cshtml.72cecc2a.cs (it was at AppData\Local\Temp\Temporary ASP.NET Files\root) and changed ASP.global_asax to System.Web.HttpApplication and the error went away.

This is the generated code:

protected static ASP.global_asax ApplicationInstance {
    get {
        return ((ASP.global_asax)(Context.ApplicationInstance));
    }
}

I changed it to:

protected static System.Web.HttpApplication ApplicationInstance {
    get {
        return ((System.Web.HttpApplication)(Context.ApplicationInstance));
    }
}

I don't know why this is happening though.

Orlando Osorio
  • 3,116
  • 7
  • 29
  • 52
  • 4
    this works but after update the file the problems appears again. see my answer for a solution, this is a known issue with visual studio 2015 – Bart Calixto May 28 '15 at 09:18
  • Anyone figure out why this happens? This was the only solution out of all that actually worked for me – Kellen Stuart Jun 07 '19 at 20:05
16

I've found that if I have the files in App_Code open in Visual Studio 2015 when I run the build, then I don't get the errors. As soon as I close the files the errors show up again.

Lance Fisher
  • 25,684
  • 22
  • 96
  • 122
  • the opposite is true for me. if the file is open it tells me it can't find system.Web.Mvc, if its closed it finds it. – odyth Mar 15 '16 at 02:04
  • I have 1 cshtml (Content.cshtml) file there but I can't open it inside VS15, don't know why ("The operation could not be completed"). I've rename it to Content.cshtml.old but that causes another problem. In the end I've deleted the App_Code folder and created a new one. Re-opened visual studio and it works now. – ranbuch Jun 02 '16 at 07:07
4

After trying a variety of the solutions proposed here, it turns out that simply quitting Visual Studio 2015 and then restarting it was enough to get the Solution/Project to successfully compile once again.

starlocke
  • 3,407
  • 2
  • 25
  • 38
  • Simply restarting Visual Studio worked for me as well. I rec'd 7 of these errors on build and after restarting VS the build succeeded. Hm. A happy ending at least, but a lingering "why?" persists. – ewomack Nov 16 '16 at 21:53
1

Two things i did to resolve this was to re-target the framework and then changing it back (was using 4.5.1 changed it to 4.5 and back)

After this i had a lot of error with T4MVC which i was also using, i upgraded it to newest version but i could see that in source control that T4MVC was installed targeting framework 4.5 and when you re/install a new nuget package it changes the target framework, so that was changed to 4.5.1

After this everything worked. I didn't test if you only need to do that second part of this answer, but i would try that first.

Jim Wolff
  • 5,052
  • 5
  • 34
  • 44
0

The way I resolved this error was to move my code form Global.asax.cs into Startup.cs and delete Global.asax so that there aren't any codebehind files in my project. See this question for more info.

Community
  • 1
  • 1
rfoster
  • 1
  • 1
0

I installed Microsoft.CodeDom.Providers.DotNetCompilerPlatform for my web project and then removed the package. The error went away.

0

JUST RIGHT CLICK ON YOUR SOLUTION PROJECT AND CLICK ON CLEAN. I HAVE DONE THAT AND IT DID WORK FOR ME. I AM SHARING THIS BECAUSE WHO KNOWS IF IT HELPS SOMEONE ELSE LIKE IT HELPS ME.

Af'faq
  • 501
  • 3
  • 13
  • 28
0

I had this same issue on a project in VS 2019 and when I upgraded to VS 2022. What fixed it for me was modifying the VS installation and installing the ".NET Compiler Platform SDK" under the Individual components area. I already had "C# and Visual Basic Roslyn compilers" selected.

I found the selection by searching for "compiler". enter image description here

jschay
  • 31
  • 2
-2

Code placed in App_Code folder is treated a bit different, it is compiled at runtime. cshtml file is 'Content' so that could be the reason why you get errors. Create normal folder within your project name it Code or something similar (but not App_Code) and place your file there.

PrzemG
  • 775
  • 6
  • 7
  • 2
    but if I take it out of `App_Code` it won't be recognized on my Razor pages because from docs @functions to be 'shared' should be on that folder. Removing from that folders ends up with `The name 'RazorFunctions' does not exist in the current context`. – Bart Calixto Feb 14 '15 at 08:34
  • confirmed...without having to do any other complex operations, just moving said file from App_Code that attempts to provide additional display helpers to other areas of the application are no longer available and errors are thrown when one attempts to access the associated 'DisplayHelper' class. – beauXjames Dec 21 '15 at 21:18