RazorGenerator can't see custom cshtml helper
Asked Answered
E

5

18

I'm having a problem with RazorGenerator: it can't compile views that uses my custom helper:

App_Code/ViewHelper.cshtml

@helper test(System.Web.Mvc.HtmlHelper html)
{
    <h4>Test</h4>    
}

Views/Test.cshtml

(...)
@ViewHelper.test(this.Html)
(...)

When I try to compile my project I get this error for Test.cshtml:

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

I tried adding various namespaces to my Views/web.config file:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.Optimization" />
        <add namespace="MyProject" />
        <add namespace="Kendo.Mvc.UI" />
        <add namespace="MvcSiteMapProvider.Web.Html" />
        <add namespace="MvcSiteMapProvider.Web.Html.Models" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

but with no success...

Am I missing some namespace that should be included in this file? If not, is there any other way to make RazorGenerator compile the view? Also, does cshtml helpers even exist in a namespace?

Egocentric answered 11/2, 2015 at 23:2 Comment(1)
The error states 'ViewHelpers' with an S at the end. Are you sure you didn't wrote @ViewHelpers.test(this.Html)?Trauma
C
12

Solution:

You need to put this comment at the beginning of a helper you have in App_Code:

@*
    Generator: MvcHelper
    GeneratePrettyNames : true
*@

Then in web.config where you have configuration for razor pages you need to add namespace that RazorGenerator generated for those helpers:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
        <namespaces>                
            <add namespace="YourWebAppNamespace.App_Code" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

Change YourWebAppNamespace to you default project namespace (it's probably your project name):

Default namespace for project in Visual Studio


Explanation:

RazorGenerator treated you helpers like normal razor view so the generated code looked like this:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "2.0.0.0")]
    [System.Web.WebPages.PageVirtualPathAttribute("~/App_Code/TestHelper.cshtml")]
    public partial class _App_Code_TestHelper_cshtml : Komplett.Chrome.Web.Features.Shared.BaseView<dynamic>
    {

#line 3 "..\..\App_Code\TestHelper.cshtml"
public System.Web.WebPages.HelperResult HelperName(string name) {

You need to add these directive comments to tell razor generator to create class with "normal" name (the same as helper file name, you do this with GeneratePrettyNames directive) and with static public function so it could be used in other views (done by Generator: MvcHelper directive). With those directive comments RazorGenerator generate C# file like this:

    [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "2.0.0.0")]
    public class TestHelper : System.Web.WebPages.HelperPage
    {

#line 6 "..\..\App_Code\TestHelper.cshtml"
public static System.Web.WebPages.HelperResult HelperName(string name) {

You now just need to add namespace to web.config so C# generated code for other views would have using statement with namespace of this generated helper.

Chrysalis answered 4/12, 2015 at 15:8 Comment(2)
In my case, I also had to change the "Build Action" of the generated helper cs file to "Compile" (it was set to "Content")Croce
@youen, I use RazorGenerator.MsBuild not VS plugin for generating code so I didn't had this problem. :) I did as described hereChrysalis
C
1

I had the same problem with RazorGenerator when I updated to MVC 5.

According to this discussion on RazorGenerator's codeplex, it seems that the RazorGenerator.MsBuild package sometimes has a problem resolving the correct version of Razor to use, so ends up missing the namespaces defined in the web.config.

To force it to use the correct version of Razor (and hopefully pick up your namespaces), create a file called razorgenerator.directives in the project folder containing the text: RazorVersion: 3

Chios answered 4/10, 2015 at 22:49 Comment(0)
B
0

Look at the actual cs-file generated in the obj\CodeGen folder for the helpers. It might be the class-name issue, so add this to the top of your helper cshtml-file:

@* GeneratePrettyNames : true *@
Bookstore answered 16/2, 2015 at 21:37 Comment(0)
E
0

It turns out that I needed to compile the helper itself to make it working.

That didn't get me far though: RazorGenerator couldn't "see" Mvc namespace references. I've read that RG is not compatible with MVC version 5.0 or higher (or is it?).

Egocentric answered 26/2, 2015 at 10:46 Comment(0)
J
0

I'm using RazorGenerator.MsBuild to compile my views and RazorGenerator.Mvc to load them. The easiest App_Code fix for me was to add the following to the top of my helper files

@*
Generator: MvcHelper
GeneratePrettyNames: true
Namespace: My.Projects.Namespace
*@

With this there's no web.config changes or anything else that needed to be made.

Jeter answered 8/10, 2016 at 23:15 Comment(1)
I also needed to append RazorVersion: 3, or the project didn't compile at first attempt (strangely, it would compile on subsequent attempts)Jointress

© 2022 - 2024 — McMap. All rights reserved.