剃刀在类库,智能感知缺失 [英] Razor in class library, missing intellisense

查看:159
本文介绍了剃刀在类库,智能感知缺失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想包括剃须刀(CSHTML)在类库文件,将包含在一个单独的项目MVC4。我已经得到的一切工作,除了智能感知似乎缺少某些类型的,尤其是 System.Web.Helpers.Json ,虽然有可能是其他人我还没有发现。我的问题可能与剃刀观点:智能感知与C#3工作类库但它并不完全一样。下面是从我的Razor视图的例子:

I'm trying to include razor (cshtml) files in a class library, to be included in a separate MVC4 project. I've gotten everything working, except intellisense seems to be missing for certain types, specifically System.Web.Helpers.Json, although there could be others I haven't discovered yet. My problem might be related to Razor views: Intellisense not working with C# 3 for class libraries but it's not exactly the same. Here is a sample from my razor view:

@model dynamic
@{
    // ... some code ...
    var options = new global::System.Web.Mvc.SelectListItem[] 
    {
        new global::System.Web.Mvc.SelectListItem() 
        {
            Text = "No",
            Value = global::System.Web.Helpers.Json.Encode(false)
        },
        new global::System.Web.Mvc.SelectListItem() 
        {
            Text = "Yes",
            Value = global::System.Web.Helpers.Json.Encode(true)
        }
    };
}
@(global::System.Web.Mvc.Html.SelectExtensions.DropDownList(this.Html, string.Empty, options))

当我最初打开剃须刀文件,我会看到几个警告几种类型的错误:

When I initially open a razor file, I will see several warnings several types of errors:


  • 类型或命名空间名称'动态'找不到(是否缺少
    集引用?)

  • 功能'隐式类型的局部变量不能使用,因为它不是一部分
    在ISO-2 C#语言规范

  • 功能'对象初始化'不能使用,因为它不是一部分...

  • 类型或命名空间名称'的Json'不命名空间中存在'System.Web.Helpers
    (是否缺少程序集引用?)

前两三种类型的错误消失,当我生成项目,但最后一个将持续存在。当我输入 System.Web.Helpers出现在智能感知。仅包含防伪 UnvalidatedRequestValues​​ 验证。当我在同一个code在cs文件类型,我看的Json 和所有我希望看到的其他选项。当我将这些文件复制到我的实际MVC项目,它不会显示在编辑器中的任何错误并运行就好了。

The first two three types errors go away when I build the project, but the last one will persist. The intellisense appears when I type System.Web.Helpers. contains only Antiforgery, UnvalidatedRequestValues, and Validation. When I type in the same code in a .cs file, I see Json and all the other options I would expect to see. And when I copy these files into my actual MVC project, it doesn't show any errors in the editor and runs just fine.

下面是我添加到我的项目的Web.config文件中得到它的工作这么远:

Here is the Web.config file I've added to my project to get it working this far:

<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" />
        <add namespace="System.Web.Helpers" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <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" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>

    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

如何让智能感知认识到这一类?

How do I get intellisense to recognize this class?

推荐答案

在使用的动态的你失去了智能感知的好处。你应该尝试使用强类型的视图。在控制器,尝试通过在那里你模型实例通过查看方法的重载指定模式。这台ViewData.Model属性来传递到视图法的价值的价值。然后指示什么类型的模型是使用@model声明的看法。您可能需要提供模型类型的完全限定类型名称。

When using dynamic you lose the benefits of Intellisense. You should try using a strongly-typed view. In the Controller, try specifying the model via an overload of the View method where you pass in the model instance. This sets the value of the ViewData.Model property to the value passed into the View method. Then indicate to the view what type of model is using the @model declaration. You may need to supply the fully qualified type name of the model type.

这篇关于剃刀在类库,智能感知缺失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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