C# 如何在 mvc 4 的 web.config 文件中添加程序集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9456207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:29:30  来源:igfitidea点击:

How to add assembly in web.config file of mvc 4

c#asp.net-mvcasp.net-mvc-4web-config

提问by Mohammad Zare

I have a "MVC 4"project and i want to add an assembly to web.config file but i don't know where should i put it. I try any ways but i cant find the solution.

我有一个“MVC 4”项目,我想向 web.config 文件添加一个程序集,但我不知道应该把它放在哪里。我尝试了任何方法,但找不到解决方案。

every time i got this error:

每次我收到此错误时:

You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

您必须添加对程序集“System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”的引用。

this is my web.config file:

这是我的 web.config 文件:

    <?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=152368
  -->
<configuration>
  <connectionStrings>
    <add name="ParsGraphicEntities" connectionString="metadata=res://*/Entities.ParsGraphic.csdl|res://*/Entities.ParsGraphic.ssdl|res://*/Entities.ParsGraphic.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=ParsGraphic;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-UI.MVC.InternetApplication-2012317193118;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="true" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <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.WebPages" />
      </namespaces>
    </pages>
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </sessionState>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

please help me.

请帮我。

thanks.

谢谢。

采纳答案by Alfonso Mu?oz

It is quite simple. Look at your compilation section in your web.config:

这很简单。查看 web.config 中的编译部分:

<compilation debug="true" targetFramework="4.0"/>

Now open the section and put your assembly inside like this:

现在打开该部分并将您的程序集放入其中,如下所示:

<compilation debug="true" targetFramework="4.0">
  <assemblies>
    <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </assemblies>
</compilation>

Libraries in this section are checked in compilation time, it has nothing to do with runtime binding.

本节中的库是在编译时检查的,与运行时绑定无关。

回答by mageeky90

I just experienced the same issue with MVC 4 in VS Express 2012 for Web, but, while Alfonso's answer was much more helpful than anything else I found on the Web, it unfortunately did not clear up the error in my project. After doing some research on the Internet (to no avail) and then just poking around Visual Studio, I found the following solution.

我刚刚在 VS Express 2012 for Web 中遇到了与 MVC 4 相同的问题,但是,虽然阿方索的回答比我在网上找到的任何其他答案都更有帮助,但不幸的是它没有清除我项目中的错误。在互联网上做了一些研究(无济于事)然后只是在 Visual Studio 周围闲逛后,我找到了以下解决方案。

In addition to adding EntityFramework to my project's references and adding the tag to my Web.config, I also had to take the following steps to satisfy the compiler:

除了将 EntityFramework 添加到我的项目的引用并将标记添加到我的 Web.config 之外,我还必须采取以下步骤来满足编译器:

  1. Right-click the References folder > Add Reference...
  2. Expand Assemblies on the left side of the window and select Framework.
  3. Scroll to and select System.Data.Entity in the list of assemblies.
  4. Make sure the box next to System.Data.Entity is checked, then click 'OK'.
  5. Rebuild the project.
  1. 右键单击引用文件夹 > 添加引用...
  2. 展开窗口左侧的程序集并选择框架。
  3. 在程序集列表中滚动到并选择 System.Data.Entity。
  4. 确保选中 System.Data.Entity 旁边的框,然后单击“确定”。
  5. 重建项目。