“ASP.global_asax does not exist in the namespace A

2019-01-30 10:55发布

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)

9条回答
ら.Afraid
2楼-- · 2019-01-30 11:18

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.

查看更多
SAY GOODBYE
3楼-- · 2019-01-30 11:21

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.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-30 11:23

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.

查看更多
登录 后发表回答