noob Mama
noob Mama

Reputation: 307

Helpers with Precompiled Views

I'm using RazorGenerator to Pre-compile my views. I also want to use Global Razor helpers(defined in cshtml files in App_Code directory). However the compilation throws up with the following error

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

However the intellisense seems to detect the helper methods. I've incorporated Razor pre-compilation as per the following blog Razor Precompilation

Also I notice that the helper file KarbonHelper.cshtml is actually pre-compiled into a .cs file in the expected location

obj\CodeGen\App_Code\KoolHelper.cshtml.cs

Any ideas or suggestions are welcome

Upvotes: 4

Views: 1288

Answers (2)

Robert J
Robert J

Reputation: 754

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

    <add namespace="MyProjectBaseNamespace.App_Code" />

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

@* Generator: MvcHelper GeneratePrettyNames : true *@

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

more info about directives: https://github.com/RazorGenerator/RazorGenerator#special-razor-directives

Upvotes: 1

Simon_Weaver
Simon_Weaver

Reputation: 146000

The problem is that the App_Code code generator process (controlled by Microsoft's code) creates you a static method, whereas the RazorGenerator creates you an instance method.

I'm still looking for a fix to this that doesn't involve a horrible hack.

Upvotes: 1

Related Questions