什么是web.config中编译/组件的元素呢?组件、元素、web、config

2023-09-02 10:55:40 作者:爱到最美是陪伴

我有一个问题,即,工作的一个新的MVC项目,只要我说Html.Partial(姓名),以我的_Layout.cshtml网页,ASP.NET会扔在页面加载此异常:

I had a problem where, working on a new MVC project, as soon as I added Html.Partial("Name") to my _Layout.cshtml page, ASP.NET would throw this exception on page load:

错误CS0012:类型System.Object的不在一个引用的程序集定义。添加一个引用到大会System.Runtime,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a补充说。

error CS0012: The type 'System.Object' is not in a referenced assembly is defined. Add a reference to the Assembly "System.Runtime, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a" added.

解决方法是将它添加到Web.config,编译元素:

The fix is to add this to the web.config, compilation element:

  <assemblies>
    <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </assemblies>

我很想知道为什么这个修复该问题。

I'd love to know why this fixes the issue.

有什么奇怪的是,新的MVC应用程序我使用的是有工作的一种享受的WebForms ASPX页面负载。我已经复制了大量的遗留WebForms的东西到这个新的MVC项目,千亩code和一切正常,并没有Html.Partial电话,连我的MVC视图渲染。

What's odd is that the new MVC app I'm using has a load of WebForms ASPX pages that work a treat. I've copied a ton of legacy WebForms stuff into this new MVC project, acres of code, and everything works, and without the Html.Partial call, even my MVC view renders.

因此​​,地球上到底是怎么回事?这是什么配置做的,为什么不正常的项目引用和目标框架的设置还不够吗?

So what on Earth is going on? What does this config do and why aren't the normal project references and target framework settings enough?

推荐答案

这是由于建造时间和ASP.NET/MVC项目的运行时环境之间的脱节和局限性。大多数code在Web项目被编译在构建时(即F5)与例外的意见(包括剃须刀和内联ASPX code)的动态编译在运行时(即在IIS)。

This is caused by a disconnect and limitation between the build time and the runtime environments of an ASP.NET/MVC project. Most code in a Web project is compiled at build time (ie F5) with exception to views (both Razor and inline ASPX code) which are built dynamically at runtime (ie within IIS).

在大多数情况下,我们试图使这两个环境的完全的相同,你通常不会注意到任何区别。不幸。你碰到这些情况下,有的一个是的差异,你需要手动添加引用,使运行时的模拟构建时。

For the most part, we attempt to make these two environments exactly the same and you will not typically notice any difference. Unfortunately. you've run into one of those cases where there is a difference and you've needed to manually add a reference to make the runtime mimic build-time.

这是怎么回事,是你可能有一个便携式库的依赖性(在猜测,Microsoft.Net.Http)。便携式图书馆有一点不同,以正常的库,我们需要额外的引用,使编译器高兴。在构建时,我们发现一个依赖于一个便携式库,并自动添加所有引用到C: Program Files文件(x86)的参考大会微软 Framework.NETFramework V4.5 外立面)。不幸的是,运行时没有做到这一点,你需要手动添加这些引用,直到我们解决这个问题在将来的版本。

What's happening, is that you likely have a dependency on a portable library (at a guess, Microsoft.Net.Http). Portable Libraries are a little different to normal libraries, and we need extra references to make the compiler happy. At build-time we detect a dependency on a portable library and automatically add all the references to C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.5Facades). Unfortunately, the runtime doesn't do this and you need to manually add these references until we fix this in a future release.

对于未来的参考,你可以抓住以下部分,直接粘贴到Web.Config中(这种重presents快照4.5):

For future reference, you can just grab the following section and paste this directly into Web.Config (this represents the snapshot for 4.5):

 <compilation>
      <assemblies>
        <add assembly="System.Collections, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Collections.Concurrent, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ComponentModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ComponentModel.Annotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ComponentModel.EventBasedAsync, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Diagnostics.Contracts, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Diagnostics.Debug, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Diagnostics.Tools, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Diagnostics.Tracing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Dynamic.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Globalization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Linq.Expressions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Linq.Parallel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Linq.Queryable, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Net.NetworkInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Net.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Net.Requests, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ObjectModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Reflection, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Reflection.Emit, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Reflection.Emit.ILGeneration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Reflection.Emit.Lightweight, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Reflection.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Reflection.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Resources.ResourceManager, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Runtime.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Runtime.InteropServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Runtime.InteropServices.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Runtime.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Runtime.Serialization.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Runtime.Serialization.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Runtime.Serialization.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Security.Principal, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ServiceModel.Duplex, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ServiceModel.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ServiceModel.NetTcp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ServiceModel.Primitives, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.ServiceModel.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Text.Encoding, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Text.Encoding.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Text.RegularExpressions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Threading, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Threading.Tasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Threading.Tasks.Parallel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Xml.ReaderWriter, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Xml.XDocument, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <add assembly="System.Xml.XmlSerializer, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
      </assemblies>
    </compilation>