需要的Razor视图引擎自动完成,以在类库工作? [英] Need razor view engine auto-complete to work in a class library?

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

问题描述

我们有一个模块化的架构,我们有一个单独的项目(类库)的一些看法(CSHTML)文件。我们怎样才能得到的语法高亮和自动完成时该项目不是一个MVC项目工作?

请注意,类库有控制器,视图,模型等,只是没有web.config中,Global.asax的,等一个正常​​的MVC项目将有。

智能感知适用于一切,但如此重要的模式:

使用MVC3 RTM,如果你将鼠标悬停在型号,你现在可以得到一个更好的错误消息:


  

C:\\ ... \\ Index.cshtml:ASP.NET运行时
  错误:没有生成提供
  注册扩展
  .cshtml。你可以注册一个在
  <编译>< buildProviders>
  在machine.config部分或
  web.config中。请确保有一个
  BuildProviderAppliesToAttribute
  属性,该属性包括值
  网络或所有。


所以我添加这样的:

 <编译>
    <&集会GT;
      <添加组件=System.Web.WebPages.Razor,版本= 1.0.0.0,文化=中性公钥= 31BF3856AD364E35/>
    < /组件>
    < buildProviders>
      <添加
         扩展=。CSHTML
         键入=System.Web.WebPages.Razor.RazorBuildProvider,System.Web.WebPages.Razor/>
    < / buildProviders>
  < /编译>

然后加入生成提供后,会出现此错误消息:


  

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



解决方案

从webconfig <一个href=\"http://blogs.msdn.com/b/webdevtools/archive/2011/01/20/how-to-get-razor-intellisense-for-model-in-a-class-library-project.aspx\"相对=nofollow>这个帖子会工作。我下面复制它(为后人):

&LT;?XML版本=1.0&GT?;
&LT;结构&gt;    &LT; configSections&GT;
        &LT; sectionGroup NAME =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;节名称=页面TYPE =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;

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?

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:

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

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'.

So I added this:

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

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

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