RazorGenerator.MsBuild not working with custom WebViewPage

490 views Asked by At

I am trying to get RazorGenerator.MsBuild to compile our CSHTML files on build. The issue I am having is that CSHTML pages that inherit from a custom WebViewPage throw a compile error. These CSHTML files work at run time if they are copied to the website, it is only the build that fails.

For example we might have the following CSHTML file:

public class MyWebViewPage : System.Web.Mvc.WebViewPage
{
    public bool DoSomething()
    {
        return true;
    }
}

And the following CSHTML:

@inherits MyWebViewPage 

@if(DoSomething()){

}

This will cause the following error:

The name 'DoSomething' does not exist in the current context.

How can I get the MSBuild task to recognise my custom class?

1

There are 1 answers

0
Michael Edwards On

This turns out to be a problem with the way that RazorGenerator.MsBuild generates code. When compiling it writes the inherited class as:

MyView : MyWebViewPage<dynamic>

The generator requires a generic base view. The simplest solution is to alter MyWebViewPage to support generics:

public class MyWebViewPage<T> : System.Web.Mvc.WebViewPage<T>
{
    public bool DoSomething()
    {
        return true;
    }
}