Assistants with Preset Views

I use RazorGenerator to precompile my views. I also want to use Global Razor helpers (defined in cshtml files in the App_Code directory). However, compilation is generated with the following error

The name "KarbonHelper" does not exist in the current context

However, intellisense seems to detect helper methods. I turned on Razor precompilation on my next blog , Razor Precompilation

I also notice that the KarbonHelper.cshtml helper file is actually precompiled into a .cs file in the expected location

obj\CodeGen\App_Code\KoolHelper.cshtml.cs 

Any ideas or suggestions are welcome.

+4
source share
2 answers

The problem is that the App_Code code generation process (managed by Microsoft code) creates a static method for you, while RazorGenerator creates an instance method for you.

I'm still looking for a fix that doesn't involve a terrible hack.

+1
source

First try adding this in the namespace section of the system.web.webPages.razor section to the Web.config file in the Views folder (change MyProjectBaseNamespace with the name of your base project namespace - this is usually equal to the project name) or just take a look at the generated KoolHelper.cshtml.cs file, which namespace was used).

  <add namespace="MyProjectBaseNamespace.App_Code" /> 

If there are still errors, try adding this directive to the first line of the helper in App_Code :

 @* Generator: MvcHelper GeneratePrettyNames : true *@ 

RazorGenerator will then generate a class that inherits System.Web.WebPages.HelperPage with static @helper methods and with the class name the same as the file name.

Additional directive information: https://github.com/RazorGenerator/RazorGenerator#special-razor-directives

+1
source

All Articles