.NET - 剃须刀外MVC应用程序 - 与去除@inherits并提供@model问题 [英] .NET - Razor outside MVC application - Problems with removing @inherits and providing @model
问题描述
对不起,我长的问题。我把它分成可单独读取三个问题。如果你能帮助我有一个问题,请你!
我有一个适当的定义实现剃刀引擎。所有作品和模板被编译和可以使用。有一些实现在眼前,涉及具有通用型号
属性,它允许强类型的视图(模板)一个基类。在这一点上,我使用的是 @inherits
指令来定义的基类,它的泛型类型。
由GVS在这里(<一做答案href="http://stackoverflow.com/questions/4448740/hosting-the-razor-view-engine-using-a-view-model">Hosting剃刀视图使用视图模型引擎),他说,使用 @model
实际上是速记 @inherits类&LT; ModelType&GT;
让我觉得两者可以互换,但事实并非如此。
这是我的模板
@inherits RazorEngine.TemplateBase&LT; MyProject.TestModel&GT;
@functions {
}
&LT; H1&GT; @ Model.TestProperty
收藏
- 删除
@inherits
指令 - 添加
@model
指令
问题
现状:一切编译和模板都可以使用。但是我有一个智能感知错误在 @inherits
指令:
没有注册的扩展名.cshtmlbuildprovider。您可以在在machine.config或web.config中注册一个。
什么是错在这里?
我的意见的web.config文件夹,如下图所示:
&LT; XML版本=1.0&GT?;
&lt;结构&GT;
&LT; configSections&GT;
&LT; sectionGroup名=system.web.webPages.razorTYPE =System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup,System.Web.WebPages.Razor,版本= 1.0.0.0,文化=中性公钥= 31BF3856AD364E35 &GT;
&lt;节名称=主机TYPE =System.Web.WebPages.Razor.Configuration.HostSection,System.Web.WebPages.Razor,版本= 1.0.0.0,文化=中性公钥= 31BF3856AD364E35requirePermission =假 /&GT;
&lt;节名称=页面类型=System.Web.WebPages.Razor.Configuration.RazorPagesSection,System.Web.WebPages.Razor,版本= 1.0.0.0,文化=中性公钥= 31BF3856AD364E35requirePermission =假 /&GT;
&LT; / sectionGroup&GT;
&LT; / configSections&GT;
&LT; system.web.webPages.razor&GT;
&LT;主机factoryType =System.Web.Mvc.MvcWebRazorHostFactory,System.Web.Mvc,版本= 3.0.0.0,文化=中性公钥= 31BF3856AD364E35/&GT;
&LT;网页pageBaseType =System.Web.Mvc.WebViewPage&GT;
&LT;命名空间&GT;
&LT;加上命名空间=System.Web.Mvc/&GT;
&LT;加上命名空间=System.Web.Mvc.Ajax/&GT;
&LT;加上命名空间=System.Web.Mvc.Html/&GT;
&LT;加上命名空间=System.Web.Routing/&GT;
&LT; /命名空间&GT;
&LT; /页&GT;
&LT; /system.web.webPages.razor>
&LT;的System.Web&GT;
&LT;编译targetFramework =4.0&GT;
&LT;组件&GT;
&LT;添加组件=System.Web.Abstractions,版本= 4.0.0.0,文化=中性公钥= 31bf3856ad364e35/&GT;
&LT;添加组件=System.Web.Routing,版本= 4.0.0.0,文化=中性公钥= 31bf3856ad364e35/&GT;
&LT;添加组件=System.Data.Linq程序,版本= 4.0.0.0,文化=中性公钥= B77A5C561934E089/&GT;
&LT;添加组件=System.Web.Mvc,版本= 3.0.0.0,文化=中性公钥= 31bf3856ad364e35/&GT;
&LT; /组件&GT;
&LT; /编译&GT;
&LT; /system.web>
&LT; /结构&gt;
随着心愿#1:
删除 @inherits
指令使得基类无形到Visual Studio的。型号
属性,因此会导致错误= > 答案/解决方案是实现心愿#2?
随着心愿#2:
添加 @model
指令引发的 @ Model.TestProperty
智能感知错误(离开 @inherits
指令的地方...):
的名称型号并不在当前上下文中存在
额外的信息:
我用下面的code实例从编译的程序集的模板。
VAR模板=(RazorTemplateBase&LT; TModel的&GT;)Container.CompiledTemplates.CreateInstance(myNameSpace对象。+ entry.TemplateName +模板);
template.Model =模型;
template.DataSource =数据源;
template.Execute();
无功输出= template.Buffer.ToString();
template.Buffer.Clear();
返回输出;
-
您可以添加引用
System.Web.WebPages.Razor.dll
(在&LT;组件&GT;
)。
它注册了RazorBuildProvider
在[preApplicationStartMethod]
。 -
的
@model
指令是独一无二的MVC 。
你需要使用MvcRazorEngineHost
。
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
- Remove the
@inherits
directive - 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;
you can add a reference to
System.Web.WebPages.Razor.dll
(in<assemblies>
).
It registers theRazorBuildProvider
in a[PreApplicationStartMethod]
.the
@model
directive is unique to MVC.
You need to use theMvcRazorEngineHost
.
这篇关于.NET - 剃须刀外MVC应用程序 - 与去除@inherits并提供@model问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!