"ASP.global_asax does not exist in the namespace ASP"

C#asp.netasp.net MvcRazor

C# Problem Overview


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)

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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.

Solution 3 - C#

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.

Solution 4 - C#

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.

Solution 5 - C#

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.

Solution 6 - C#

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.

Solution 7 - C#

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

Solution 8 - C#

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.

Solution 9 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBart CalixtoView Question on Stackoverflow
Solution 1 - C#Bart CalixtoView Answer on Stackoverflow
Solution 2 - C#Orlando WilliamView Answer on Stackoverflow
Solution 3 - C#Lance FisherView Answer on Stackoverflow
Solution 4 - C#starlockeView Answer on Stackoverflow
Solution 5 - C#Jim WolffView Answer on Stackoverflow
Solution 6 - C#rfosterView Answer on Stackoverflow
Solution 7 - C#spine4illerView Answer on Stackoverflow
Solution 8 - C#Af'faqView Answer on Stackoverflow
Solution 9 - C#PrzemGView Answer on Stackoverflow