需要剃刀视图引擎自动完成才能在类库中工作吗? [英] Need razor view engine auto-complete to work in a class library?

查看:23
本文介绍了需要剃刀视图引擎自动完成才能在类库中工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个模块化架构,我们在一个单独的项目(类库)中有一些视图 (cshtml) 文件.当项目不是 MVC 项目时,我们如何让 语法突出显示和 自动完成工作?

We have a modular architecture where we have some views (cshtml) files in a separate project (class library). How can we get the syntax highlighting and autocomplete to work when the project isn't an MVC project?

请注意,类库具有控制器、视图、模型等.它只是没有普通 mvc 项目应具有的 web.config、global.asax 等.

Please note that the class library has controllers, views, models etc. It just doesn't have the web.config, global.asax, etc that a normal mvc project would have.

智能感知适用于除如此重要的模型之外的所有内容:

The intellisense works for everything but the so important model:

使用 MVC3 RTM,如果您将鼠标悬停在模型上,您现在可以获得更好的错误消息:

With MVC3 RTM, if you hover over the Model, you can now get a better error message:

C:...Index.cshtml:ASP.NET 运行时错误:没有构建提供程序注册延期'.cshtml'.您可以在<编译>machine.config 中的部分或网络配置.确保有一个BuildProviderAppliesToAttribute包含值的属性网络"或全部".

C:...Index.cshtml: ASP.NET runtime error: There is no build provider registered for the extension '.cshtml'. You can register one in the <compilation><buildProviders> section in the machine.config or web.config. Make sure is has a BuildProviderAppliesToAttribute attribute which includes the value 'Web' or 'All'.

所以我添加了这个:

<compilation>
    <assemblies>
      <add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </assemblies>
    <buildProviders>
      <add
         extension=".cshtml"
         type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
    </buildProviders>
  </compilation>

然后添加构建提供程序后,出现此错误消息:

Then after adding the build provider, this error message appears:

C:...Index.cshtml:ASP.NET 运行时错误:无法加载文件或程序集'System.Web.WebPages.Razor' 或其中之一它的依赖.系统不能找到指定的文件.(C:...machine.config 第 259 行)

C:...Index.cshtml: ASP.NET runtime error: Could not load file or assembly 'System.Web.WebPages.Razor' or one of its dependencies. The system cannot find the file specified. (C:...machine.config line 259)

推荐答案

来自 这篇文章 会起作用.我已将其复制到下面(供后代使用):

The webconfig from this post will work. I've copied it below (for posterity):

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

这篇关于需要剃刀视图引擎自动完成才能在类库中工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆