MVC3视图无法找到添加的引用类

时间:2022-06-11 17:23:24

My view is having issues finding a class located in one of my references. This reference is to a dll built outside of the project.

我的观点是在我的一个参考文献中找到一个类时遇到问题。此引用是在项目外部构建的dll。

The view always gives error:

视图总是出错:

The type or namespace name 'Company' could not be found (are you missing a using directive or an assembly reference)

找不到类型或命名空间名称“Company”(您是否缺少using指令或程序集引用)

Here is the controller. No issues there.

这是控制器。那里没有问题。

using Company.Entities;

public ActionResult Find()
{
    Person test = Person.SelectByADDistinguishedName("L*", false);
    return View(test);
}

Here is the View. The error occurs in the @Model line.

这是视图。该错误发生在@Model行中。

@model Company.Entities.Person

@{
    ViewBag.Title = "Bob";
}

<h2>Find</h2>

My Views/Web.config currently looks like this

我的Views / Web.config目前看起来像这样

  <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="Company.Entities" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

I've checked similar threads like this one but to no avail.

我已经检查了类似这样的线程,但无济于事。

Here is the message on the screen

这是屏幕上的消息

Line 25:     using System.Web.Mvc.Html;
Line 26:     using System.Web.Routing;
Line 27:     using Company.Entities;
Line 28:     
Line 29:     


Source File: c:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET

Files\root\49c9c7db\1f9dd5c8\App_Web_find.cshtml.a8d08dba.xro6gxci.0.cs Line: 27

Files \ root \ 49c9c7db \ 1f9dd5c8 \ App_Web_find.cshtml.a8d08dba.xro6gxci.0.cs行:27

If I strip out any mention of the assembly (out of web.config - no @using statments). I get the same error message when loaded but against this line

如果我删除任何提及的程序集(在web.config之外 - 没有@using statments)。我在加载时会收到相同的错误消息但是在此行上

public class _Page_Views_Home_Find_cshtml : System.Web.Mvc.WebViewPage<Company.Entities.Person> {

3 个解决方案

#1


7  

After working it with it for awhile, I ended up solving it.

使用它一段时间后,我最终解决了它。

I had to add the assembly under the assemblies section of the main web.config (not the one under the views)

我不得不在主web.config的assembly部分下添加程序集(不是视图下的那个)

Sample of the web.config.

web.config的示例。

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <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" />
    <add assembly="Company, Version=2.0.31.0, Culture=neutral, PublicKeyToken=df9010405db60e6d"/>
  </assemblies>
</compilation>

Thanks for people who gave me suggestions.

感谢为我提供建议的人。

#2


4  

Be sure to check your views directory for an additional web.config file. I seem to remember running into a similar problem because I hadn't updated the correct web.config. YMMV.

请务必检查您的views目录以获取其他web.config文件。我似乎记得遇到类似的问题因为我没有更新正确的web.config。因人而异。

#3


2  

Adding the reference

添加参考

before where you use your control, in .cshtml file works. Example:

在使用您的控件之前,在.cshtml文件中有效。例:

@model IEnumerable<MyApp.Models.MyModel>
@using System.Web.UI.WebControls;

But you can put it in web.config, under namespaces tag. This way it is global to all views. Also, there is no need to specify the version:

但是您可以将它放在名称空间标记下的web.config中。这样,它对所有视图都是全局的。此外,无需指定版本:

<namespaces>
        <add namespace="System.Web.UI.WebControls" />
</namespaces>

#1


7  

After working it with it for awhile, I ended up solving it.

使用它一段时间后,我最终解决了它。

I had to add the assembly under the assemblies section of the main web.config (not the one under the views)

我不得不在主web.config的assembly部分下添加程序集(不是视图下的那个)

Sample of the web.config.

web.config的示例。

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <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" />
    <add assembly="Company, Version=2.0.31.0, Culture=neutral, PublicKeyToken=df9010405db60e6d"/>
  </assemblies>
</compilation>

Thanks for people who gave me suggestions.

感谢为我提供建议的人。

#2


4  

Be sure to check your views directory for an additional web.config file. I seem to remember running into a similar problem because I hadn't updated the correct web.config. YMMV.

请务必检查您的views目录以获取其他web.config文件。我似乎记得遇到类似的问题因为我没有更新正确的web.config。因人而异。

#3


2  

Adding the reference

添加参考

before where you use your control, in .cshtml file works. Example:

在使用您的控件之前,在.cshtml文件中有效。例:

@model IEnumerable<MyApp.Models.MyModel>
@using System.Web.UI.WebControls;

But you can put it in web.config, under namespaces tag. This way it is global to all views. Also, there is no need to specify the version:

但是您可以将它放在名称空间标记下的web.config中。这样,它对所有视图都是全局的。此外,无需指定版本:

<namespaces>
        <add namespace="System.Web.UI.WebControls" />
</namespaces>