2013-02-08 8 views
5

Tôi đang cố gắng bao gồm các tệp dao cạo (cshtml) trong một thư viện lớp, để được bao gồm trong một dự án MVC4 riêng biệt. Tôi đã nhận được tất cả mọi thứ làm việc, ngoại trừ intellisense dường như bị thiếu cho một số loại, cụ thể là System.Web.Helpers.Json, mặc dù có thể có những người khác mà tôi chưa phát hiện được. Vấn đề của tôi có thể liên quan đến Razor views: Intellisense not working with C# 3 for class libraries nhưng không chính xác như vậy. Dưới đây là một ví dụ từ cái nhìn dao cạo của tôi:Dao cạo trong thư viện lớp học, thiếu intellisense

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

Khi tôi bước đầu mở một tập tin dao cạo, tôi sẽ thấy một vài cảnh báo một số loại lỗi:

  • Loại hoặc tên không gian tên 'năng động' có thể không được tìm thấy (là bạn thiếu một tài liệu tham khảo lắp ráp?)
  • tính năng 'ngầm gõ biến địa phương' không thể được sử dụng bởi vì nó không phải là một phần của tiêu chuẩn ISO-2 C đặc điểm kỹ thuật # ngôn ngữ
  • tính năng 'đối tượng initializ er 'không thể được sử dụng bởi vì nó không phải là một phần ...
  • Tên loại hoặc không gian tên' Json 'không tồn tại trong không gian tên' System.Web.Helpers ' (bạn thiếu tham chiếu assembly?)

Hai lỗi loại ba đầu tiên biến mất khi tôi xây dựng dự án, nhưng lỗi cuối cùng sẽ vẫn tồn tại. Intellisense xuất hiện khi tôi nhập System.Web.Helpers. chỉ chứa Antiforgery, UnvalidatedRequestValuesValidation. Khi tôi nhập vào cùng một mã trong tệp .cs, tôi thấy Json và tất cả các tùy chọn khác mà tôi mong đợi sẽ thấy. Và khi tôi sao chép các tệp này vào dự án MVC thực tế của tôi, nó không hiển thị bất kỳ lỗi nào trong trình chỉnh sửa và chỉ chạy tốt.

Dưới đây là file Web.config tôi đã thêm vào dự án của tôi để làm cho nó làm việc này cho đến nay:

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

Làm thế nào để nhận được IntelliSense để nhận lớp này?

Trả lời

0

Khi sử dụng động bạn mất lợi ích của Intellisense. Bạn nên thử sử dụng chế độ xem mạnh mẽ. Trong Bộ điều khiển, hãy thử xác định mô hình thông qua quá tải của phương thức Xem nơi bạn vượt qua trong cá thể mô hình. Điều này đặt giá trị của thuộc tính ViewData.Model thành giá trị được chuyển vào phương thức Xem. Sau đó, cho biết loại mô hình đang sử dụng khai báo @model. Bạn có thể cần cung cấp tên loại đủ điều kiện của loại mô hình.