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)
I had the same issue.
I manually edited the generated file
file.cshtml.72cecc2a.cs
(it was atAppData\Local\Temp\Temporary ASP.NET Files\root
) and changedASP.global_asax
toSystem.Web.HttpApplication
and the error went away.This is the generated code:
I changed it to:
I don't know why this is happening though.
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.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.