ViewBag和的HtmlHelper错误:"编译一个动态的前pression所需的一个或多个类型不能被发现。你是否缺少程序集引用&QUOT?; [英] ViewBag and HtmlHelper errors : "One or more types required to compile a dynamic expression cannot be found. Are you missing an assembly reference?"

查看:470
本文介绍了ViewBag和的HtmlHelper错误:"编译一个动态的前pression所需的一个或多个类型不能被发现。你是否缺少程序集引用&QUOT?;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ViewBag错误:编译一个动态的前pression所需的一个或多个类型不能找到你缺少程序集引用?

所有HTML辅助方法包含错误:对方法的类型参数不能从使用推断尝试显式指定类型参数。

花的最后两天通过与此错误其他职位挖后,这里是我试过:


  • 修复的Visual Studio

  • 重新安装Visual Studio

  • 确保我有Microsoft.CSharp和System.Core程序引用

  • 添加到Global.asax中:ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(新CustomRazorViewEngine());

  • 从false更改Microsoft.CSharp复制本地的参考值设置为True

  • 今天我安装了更新RC 3

  • 检查GAC的版本(仅包含版本4.0.0.0)

  • 卸载/重新安装最新版本的.NET Framework

这是我的观点的Web.Config:

 <?XML版本=1.0&GT?;<结构>
  < configSections>
    < sectionGroup NAME =system.web.webPages.razorTYPE =System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup,System.Web.WebPages.Razor,版本= 3.0.0.0,文化=中性公钥= 31BF3856AD364E35 >
      <节名称=主机TYPE =System.Web.WebPages.Razor.Configuration.HostSection,System.Web.WebPages.Razor,版本= 3.0.0.0,文化=中性公钥= 31BF3856AD364E35requirePermission =假 />
      <节名称=页面TYPE =System.Web.WebPages.Razor.Configuration.RazorPagesSection,System.Web.WebPages.Razor,版本= 3.0.0.0,文化=中性公钥= 31BF3856AD364E35requirePermission =假 />
    < / sectionGroup>
  < / configSections>  < system.web.webPages.razor>
    <主机factoryType =System.Web.Mvc.MvcWebRazorHostFactory,System.Web.Mvc,版本= 5.2.0.0,文化=中性公钥= 31BF3856AD364E35/>
    <页面pageBaseType =System.Web.Mvc.WebViewPage>
      <&命名空间GT;
        <添加命名空间=System.Web.Mvc/>
        <添加命名空间=System.Web.Mvc.Ajax/>
        <添加命名空间=System.Web.Mvc.Html/>
        <添加命名空间=System.Web.Optimization/>
        <添加命名空间=System.Web.Routing/>
        <添加命名空间=WebApplication14/>
      < /命名空间>
    < /页>
  < /system.web.webPages.razor>  <&的appSettings GT;
    <添加键=网页:启用VALUE =FALSE/>
  < /的appSettings>  < system.webServer>
    <&处理GT;
      <清除NAME =BlockViewHandler/>
      <添加名称=BlockViewHandler路径=*动词=*preCondition =integratedModeTYPE =System.Web.HttpNotFoundHandler/>
    < /处理器>
  < /system.webServer>
< /结构>

的Web.Config

 <?XML版本=1.0&GT?;
<! -
  有关如何配置ASP.NET应用程序的更多信息,请访问:
  http://go.microsoft.com/fwlink/?LinkId=301880
   - >
<结构>
  < configSections>
    <! - 有关Entity Framework的配置的详细信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
    <节名称=的EntityFrameworkTYPE =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,的EntityFramework,版本6.0.0.0 =文化=中性公钥= b77a5c561934e089requirePermission =FALSE/>
  < / configSections>
  <&是connectionStrings GT;
    <添加名称=DefaultConnection的connectionString =数据Source=(LocalDb)\\v11.0;AttachDbFilename=|DataDirectory|\\aspnet-WebApplication14-20140703071149.mdf;Initial目录= ASPNET-WebApplication14-20140703071149;集成安全性=真的providerName =System.Data.SqlClient的/>
  < /&是connectionStrings GT;
  <&的appSettings GT;
    <添加键=网页:版本值=3.0.0.0/>
    <添加键=网页:启用VALUE =FALSE/>
    <添加键=ClientValidationEnabledVALUE =真/>
    <添加键=UnobtrusiveJavaScriptEnabledVALUE =真/>
  < /的appSettings>
  <&的System.Web GT;
    <身份验证模式=无/>
    <编译调试=真targetFramework =4.0/>
    <的httpRuntime />
  < /system.web>
  < system.webServer>
    <模块>
      <清除NAME =FormsAuthentication/>
    < /模块>
  < /system.webServer>
  <&运行GT;
    < assemblyBinding的xmlns =瓮:架构 - 微软COM:asm.v1>
      < dependentAssembly>
        < assemblyIdentity名称=Newtonsoft.Json文化=中性公钥=30ad4fe6b2a6aeed/>
        < bindingRedirect oldVersion =0.0.0.0-6.0.0.0NEWVERSION =6.0.0.0/>
      < / dependentAssembly>
      < dependentAssembly>
        < assemblyIdentity名称=System.Web.Helpers公钥=31bf3856ad364e35/>
        < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>
      < / dependentAssembly>
      < dependentAssembly>
        < assemblyIdentity名称=System.Web.Mvc公钥=31bf3856ad364e35/>
        < bindingRedirect oldVersion =1.0.0.0-5.2.0.0NEWVERSION =5.2.0.0/>
      < / dependentAssembly>
      < dependentAssembly>
        < assemblyIdentity名称=System.Web.Optimization公钥=31bf3856ad364e35/>
        < bindingRedirect oldVersion =1.0.0.0-1.1.0.0NEWVERSION =1.1.0.0/>
      < / dependentAssembly>
      < dependentAssembly>
        < assemblyIdentity名称=System.Web.WebPages公钥=31bf3856ad364e35/>
        < bindingRedirect oldVersion =1.0.0.0-3.0.0.0NEWVERSION =3.0.0.0/>
      < / dependentAssembly>
      < dependentAssembly>
        < assemblyIdentity名称=WebGrease公钥=31bf3856ad364e35/>
        < bindingRedirect oldVersion =1.0.0.0-1.5.2.14234NEWVERSION =1.5.2.14234/>
      < / dependentAssembly>
    < / assemblyBinding>
  < /运行>
  <&的EntityFramework GT;
    < defaultConnectionFactory TYPE =System.Data.Entity.Infrastructure.LocalDbConnectionFactory,的EntityFramework>
      <&参数GT;
        <参数值=11.0/>
      < /参数>
    < / defaultConnectionFactory>
    <供应商>
      <供应商invariantName =System.Data.SqlClient的TYPE =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
    < /供应商>
  < /&的EntityFramework GT;
< /结构>

这是我的Global.asax:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用的System.Web;
使用System.Web.Mvc;
使用System.Web.Optimization;
使用System.Web.Routing;命名空间WebApplication14
{
    公共类MvcApplication:System.Web.HttpApplication
    {
        保护无效的Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

在真的做什么在这一点上的损失。这是打开一个全新的MVC项目以及旧的时候发生。我想通后的新安装和更新错误会消失,但我错了。任何帮助将大大AP preciated。

示例code:

  @model WebApplication14.Models.ResetPasswordViewModel
    @ {
        ViewBag.Title =重置密码;
    }    < H2方式> @ ViewBag.Title< / H>    @using(Html.BeginForm(ResetPassword,帐户,FormMethod.Post,新{@class =形横,角色=形式}))
    {
        @ Html.AntiForgeryToken()
        < H4>重置您的密码< / H4方式>
        <小时/>
        @ Html.ValidationSummary(,新{@class =TEXT-危险})
        @ Html.HiddenFor(型号=方式>模式code)
        < D​​IV CLASS =表单组>
            @ Html.LabelFor(M = GT; m.Email,新{@class =COL-MD-2控制标签})
            < D​​IV CLASS =COL-MD-10>
                @ Html.TextBoxFor(M = GT; m.Email,新{@class =表格控})
            < / DIV>
        < / DIV>
        < D​​IV CLASS =表单组>
            @ Html.LabelFor(M = GT; m.Password,新{@class =COL-MD-2控制标签})
            < D​​IV CLASS =COL-MD-10>
                @ Html.PasswordFor(M = GT; m.Password,新{@class =表格控})
            < / DIV>
        < / DIV>
        < D​​IV CLASS =表单组>
            @ Html.LabelFor(M = GT; m.ConfirmPassword,新{@class =COL-MD-2控制标签})
            < D​​IV CLASS =COL-MD-10>
                @ Html.PasswordFor(M = GT; m.ConfirmPassword,新{@class =表格控})
            < / DIV>
        < / DIV>
        < D​​IV CLASS =表单组>
            < D​​IV CLASS =COL-MD-偏移2 COL-MD-10>
                <输入类型=提交级=BTN BTN-默认值=重置/>
            < / DIV>
        < / DIV>
    }    @section脚本{
        @ Scripts.Render(〜/包/ jqueryval)
    }


解决方案

我也有这个问题。我已执行下列步骤后,这个问题是固定的:


  • 安装了Visual Studio更新3

  • 从\\ Windows下删除所有临时文件(\\ Microsoft.NET \\ Framework64 \\ v4.0.30319 \\临时ASP.NET文件,\\ WINDOWS \\ Microsoft.NET \\框架\\ v4.0.30319 \\临时ASP.NET文件,\\应用程序数据\\当地\\ TEMP \\临时ASP.NET文件\\根)

  • 删除{}项目名从项目文件夹和csproj.user
  • .v12.suo
  • 创建一个新的asp.net MVC应用程序和浏览web.config文件复制到旧的应用程序。这里有旧的(左)和新的(右)之间的差异: http://screencast.com/t/x4WisCWf

编辑:忽略来自文件的比较行20

ViewBag error: "One or more types required to compile a dynamic expression cannot be found. Are you missing an assembly reference?"

All HTML Helper methods contain the error: "The type argument for method cannot be inferred from usage. Try specifying the type arguments explicitly."

After spending the last two days digging through other posts related to this error, here is what I've tried:

  • Repairing Visual Studio
  • Re-installing Visual Studio
  • Making sure I have Microsoft.CSharp and System.Core referenced
  • Adding to global.asax: ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new CustomRazorViewEngine());
  • Changing the reference value of Microsoft.CSharp Copy Local from false to True
  • Today I installed update RC 3
  • Checking the GAC for versions (only contains version 4.0.0.0)
  • Uninstalling/Re-installing the latest version of the .NET framework

Here is my view Web.Config:

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.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=5.2.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.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="WebApplication14" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>

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

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

Web.Config:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication14-20140703071149.mdf;Initial Catalog=aspnet-WebApplication14-20140703071149;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
  </appSettings>
  <system.web>
    <authentication mode="None"/>
    <compilation debug="true" targetFramework="4.0"/>
    <httpRuntime/>
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication"/>
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.0.0" newVersion="5.2.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
  </entityFramework>
</configuration>

And here is my Global.asax:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace WebApplication14
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

Really at a loss at this point on what to do. This is happening when opening a brand new MVC project as well as old ones. I figured after the new install and updates the error would go away, but I was wrong. Any help would be greatly appreciated.

Example Code:

   @model WebApplication14.Models.ResetPasswordViewModel
    @{
        ViewBag.Title = "Reset password";
    }

    <h2>@ViewBag.Title.</h2>

    @using (Html.BeginForm("ResetPassword", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
    {
        @Html.AntiForgeryToken()
        <h4>Reset your password.</h4>
        <hr />
        @Html.ValidationSummary("", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Code)
        <div class="form-group">
            @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
            <div class="col-md-10">
                @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
            </div>
        </div>
        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" class="btn btn-default" value="Reset" />
            </div>
        </div>
    }

    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

解决方案

I also had this problem. After I had performed the following steps this issue was fixed:

EDIT : Ignore the line 20 from comparation files.

这篇关于ViewBag和的HtmlHelper错误:&QUOT;编译一个动态的前pression所需的一个或多个类型不能被发现。你是否缺少程序集引用&QUOT?;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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