简体   繁体   中英

Razor in class library, missing intellisense

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:

  • The type or namespace name 'dynamic' could not be found (are you missing an assembly reference?)
  • Feature 'implicitly typed local variable' cannot be used because it is not part of the ISO-2 C# language specification
  • Feature 'object initializer' cannot be used because it is not part ...
  • The type or namespace name 'Json' does not exist in the namespace 'System.Web.Helpers' (are you missing an assembly reference?)

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.

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM