开发者

.NET - Razor outside MVC application - Problems with removing @inherits and providing @model

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" />
                &开发者_如何学Clt;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;


  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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜