.NET - MVC 应用程序外部的 Razor - 删除 @inherits 和提供 @model 时出现问题

发布于 2024-11-06 01:33:03 字数 3966 浏览 0 评论 0原文

抱歉问了这么长的问题。我把它分成三个问题,可以分别阅读。如果您能够帮助我解决一个问题,请帮我!

我已经有了 Razor 引擎的自定义实现。所有作品和模板均已编译并可以使用。手头有一些实现,其中涉及具有通用 Model 属性的基类,该属性允许强类型视图(模板)。此时,我使用 @inherits 指令来定义基类及其泛型类型。

GVS 在这里做出的答案(使用 a view model),他说使用 @model 实际上是 @inherits Class 的简写,让我认为两者可以互换,但是这个事实并非如此。

这是我的模板

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

}
<h1>@Model.TestProperty

愿望清单

  1. 删除 @inherits 指令
  2. 添加 @model 指令

问题


当前情况: 一切都可以编译并且可以使用模板。但是,我在 @inherits 指令处出现智能感知错误:

没有为扩展名“.cshtml”注册的构建提供程序。您可以在 machine.config 或 web.config 中注册一个。

这里出了什么问题?

我的视图文件夹中有一个 web.config,如下所示:

<?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>

带有愿望清单#1: 删除 @inherits 指令会使基类的 .Model 属性对 Visual Studio 不可见,因此会导致错误 => 答案/解决方案是实施愿望清单 #2?


使用愿望清单 #2: 添加 @model 指令会在 @Model.TestProperty 上引发智能感知错误(即使保留 @inherits 指令...):

名称模型在当前上下文中不存在。

额外信息:

我使用以下代码从编译的程序集中实例化模板。

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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒浓于脸红 2024-11-13 01:33:03
  1. 您可以添加对 System.Web.WebPages.Razor.dll 的引用(在 中)。
    它注册 RazorBuildProvider [PreApplicationStartMethod] 中。

  2. @model 指令是 MVC 特有的
    您需要使用 MvcRazorEngineHost

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文