4

Sorry for the long question. I broke it up into three problems which can be read separately. If you're able to help me with one problem, please do!

I have a custom implementation of a Razor engine in place. All works and templates are compiled and can be used. There is some implementation at hand which involves a baseclass having a generic Model property that allows for strongly typed views (templates). At this point I'm using an @inherits directive to define the baseclass and it's generic type.

The answer made by GVS here (Hosting the Razor View engine using a view model), where he says that using @model is actually shorthand for @inherits Class<ModelType> makes me think the two can be interchanged, however this is not the case.

This is my template

@inherits RazorEngine.TemplateBase<MyProject.TestModel>
@functions {

}
<h1>@Model.TestProperty

Wishlist

  1. Remove the @inherits directive
  2. Add a @model directive

Problems


Current situation: Everything compiles and templates can be used. However I have an intellisense error at the @inherits directive.:

There is no buildprovider registered for the extension ".cshtml". You can register one in the in the machine.config or web.config.

What's wrong here?

I have a web.config in the views folder like below:

<?xml version="1.0"?>
<configuration>

    <configSections>
        <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
            <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
            <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
        </sectionGroup>
    </configSections>

    <system.web.webPages.razor>
        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.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" />
            </namespaces>
        </pages>
    </system.web.webPages.razor>

    <system.web>
        <compilation targetFramework="4.0">
            <assemblies>
                <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
                <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
                <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
            </assemblies>
        </compilation>
    </system.web>

</configuration>

With wishlist #1: Removing the @inherits directive makes the .Model property of the baseclass invisible to visual studio and therefore results in an error => answer/solution is to implement wishlist #2?


With wishlist #2: Adding the @model directive throws the intellisense error on @Model.TestProperty (even when leaving the @inherits directive in place...):

The name Model does not exist in the current context.

Extra info:

I'm using the following code to instantiate a template from the compiled assembly.

var template = (RazorTemplateBase<TModel>)Container.CompiledTemplates.CreateInstance("MyNamespace." + entry.TemplateName + "Template");
template.Model = model;
template.DataSource = dataSource;
template.Execute();
var output = template.Buffer.ToString();
template.Buffer.Clear();
return output;
Community
  • 1
  • 1
Ropstah
  • 17,538
  • 24
  • 120
  • 194

1 Answers1

1
  1. you can add a reference to System.Web.WebPages.Razor.dll (in <assemblies>).
    It registers the RazorBuildProvider in a [PreApplicationStartMethod].

  2. the @model directive is unique to MVC.
    You need to use the MvcRazorEngineHost.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1. Adding the reference has no effect. Still no build provider registered... 2. Do i _need_ to? Please see my extra info, i'm using that code to create the template instances. – Ropstah May 10 '11 at 14:42
  • 1
    @Ropstah: 1. Try explicitly registering the `RazorBuildProvider` in Web.config. http://msdn.microsoft.com/en-us/library/h0e51sw9.aspx – SLaks May 10 '11 at 14:53
  • 2. Yes; `@model` is not a standard Razor syntac. I believe that RazorEngine has its own implementation of it somewhere, but I don't know offhand. – SLaks May 10 '11 at 14:54
  • @SLaks: 1. works, it has to be defined in the main `web.config` though! Which i don't like because this makes my module require configuration changes instead of just a separate web.config which can be automatically inserted.... :( It works though... – Ropstah May 10 '11 at 15:03
  • 1
    @Ropstah: You should be able to do it yourself in a `[PreApplicationStartMethod]`. – SLaks May 10 '11 at 15:04
  • @SLaks that link is broken, know another? – Jeremy Holovacs Feb 28 '13 at 00:47