You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET MVC中Html辅助类报错:未引用System.Core程序集

解决ASP.NET MVC视图CS0012错误(System.Core引用问题)

问题场景

在ASP.NET MVC视图中使用以下代码时出现编译错误:

<tr>
    <td>@Html.LabelFor(LoginVM => model.UserName)</td>
    <td>@Html.TextBoxFor(model => model.UserName)</td>
    <td>@Html.ValidationMessageFor(model => model.UserName)</td>
</tr>
<tr>
    <td>@Html.LabelFor(model => model.Password)</td>
    <td>@Html.TextBoxFor(model => model.Password)</td>
    <td>@Html.ValidationMessageFor(model => model.Password)</td>
</tr>

错误信息:

Error (active) CS0012
The type 'Expression<>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 1_Views_PrismLogin_Index.cshtml C:\Users\Administrator\source\repos\PrismReplica\Views\PrismLogin\Index.cshtml 23

已尝试手动添加引用、更新包、清理重建解决方案,但问题仍存在。

解决步骤

1. 修正视图代码的语法错误

第一个LabelFor的参数存在语法错误,将LoginVM => model.UserName改为model => model.UserName,修正后的代码:

<tr>
    <td>@Html.LabelFor(model => model.UserName)</td>
    <td>@Html.TextBoxFor(model => model.UserName)</td>
    <td>@Html.ValidationMessageFor(model => model.UserName)</td>
</tr>
<tr>
    <td>@Html.LabelFor(model => model.Password)</td>
    <td>@Html.TextBoxFor(model => model.Password)</td>
    <td>@Html.ValidationMessageFor(model => model.Password)</td>
</tr>

2. 检查项目根目录web.config的程序集引用

打开项目根目录的web.config,找到<assemblies>节点,确认是否包含System.Core的引用,若没有则添加:

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.7.2">
      <assemblies>
        <add assembly="System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

3. 检查Views文件夹下web.config的命名空间

打开Views文件夹内的web.config,找到<namespaces>节点,确认已添加System.Linq命名空间:

<configuration>
  <system.web.webPages.razor>
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Linq" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <!-- 其他已有命名空间 -->
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>

4. 清理编译缓存并重建

  • 手动删除项目目录下的bin和obj文件夹
  • 在Visual Studio中选择生成 > 清理解决方案
  • 再选择生成 > 重新生成解决方案

5. 确认项目目标框架版本

右键项目 > 属性 > 应用程序,确认目标框架为.NET Framework 4.0及以上版本,若版本过低则升级到兼容版本。

内容的提问来源于stack exchange,提问作者Asmar J.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 20:52:15