简体   繁体   English

ASP.NET Core TypeLoadException无法从程序集“ System.Web”加载类型“ System.Web.PreApplicationStartMethodAttribute”

[英]ASP.NET Core TypeLoadException Could not load type 'System.Web.PreApplicationStartMethodAttribute' from assembly 'System.Web'

In the course of developing an ASP.NET Core MVC project, I have run into this exception multiple times. 在开发ASP.NET Core MVC项目的过程中,我多次遇到此异常。

It happens when POSTing back to an MVC controller. POST回MVC控制器时会发生这种情况。 Let's say the ViewModel looks like this: 假设ViewModel看起来像这样:

using System.Web;

namespace MyNamespace 
{
    public class MyViewModel 
    {
        public List<SelectListItem> MyOptions { get; set; }
    }
}

and the header for this view contains this 该视图的标题包含

@model MyNamespace.MyViewModel

As mentioned, on POST back (assuming the View has a form and a postback in it) this exception is thrown. 如前所述,在POST back(假设View中有一个窗体和一个postback)中,将引发此异常。

An unhandled exception occurred while processing the request. 处理请求时发生未处理的异常。 TypeLoadException: Could not load type 'System.Web.PreApplicationStartMethodAttribute' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. TypeLoadException:无法从程序集“ System.Web,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a”中加载类型“ System.Web.PreApplicationStartMethodAttribute”。 System.ModuleHandle.ResolveType(RuntimeModule module, int typeToken, IntPtr* typeInstArgs, int typeInstCount, IntPtr* methodInstArgs, int methodInstCount, ObjectHandleOnStack type) System.ModuleHandle.ResolveType(RuntimeModule模块,int typeToken,IntPtr * typeInstArgs,int typeInstCount,IntPtr * methodInstArgs,int methodInstCount,ObjectHandleOnStack类型)

After a couple hours of debugging, the solution turned out to be an incorrect using statement. 经过几个小时的调试,结果证明该解决方案是不正确的using语句。

SelectListItem exists in both System.Web and Microsoft.AspNetCore.Mvc.Rendering SelectListItem存在于System.WebMicrosoft.AspNetCore.Mvc.Rendering

However, .NET Core does not support some element of this when referenced from System.Web 但是,从System.Web引用时,.NET Core不支持此元素。

Simply changing the using statement to the following fixes the issue: 只需将using语句更改为以下内容即可解决此问题:

using Microsoft.AspNetCore.Mvc.Rendering;

This is likely to occur with other types, so if you're getting strange errors in ASP.NET Core MVC, it may be a good idea to check your using statements for System.Web . 其他类型可能会发生这种情况,因此,如果您在ASP.NET Core MVC中遇到奇怪的错误,则最好检查一下System.Web的using语句。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Web.PreApplicationStartMethodAttribute和ILMerge - System.Web.PreApplicationStartMethodAttribute and ILMerge 无法从程序集&#39;System.Web,...&#39;加载类型&#39;...&#39; - Could not load type '…' from assembly 'System.Web, …' 无法从程序集 System.Web 加载类型“System.Web.HttpPostedFileBase” - Could not load type 'System.Web.HttpPostedFileBase' from assembly System.Web NUnit 测试错误:无法从程序集 'System.Web,版本 = 4.0.0.0 加载类型 'System.Web.HttpApplication' - NUnit Test Error: Could not load type 'System.Web.HttpApplication' from assembly 'System.Web, Version=4.0.0.0 ASP.NET Core错误:System.TypeLoadException:无法加载类型&#39;Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream&#39; - ASP.NET Core Error: System.TypeLoadException: Could not load type 'Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream' 无法从程序集“System.Web,版本=4.0.0.0,Cult=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“System.Web.UI.ParseChildrenAttribute” - Could not load type 'System.Web.UI.ParseChildrenAttribute' from assembly 'System.Web, Version=4.0.0.0, Cult=neutral, PublicKeyToken=b03f5f7f11d50a3a' 无法从程序集“System.Web,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50 - Could not load type 'System.Web.UI.ParseChildrenAttribute' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50 System.TypeLoadException:无法从程序集“ x”加载类型“ x”吗? - System.TypeLoadException: Could not load type 'x' from assembly 'x'? System.TypeLoadException:无法从程序集“y”加载类型“x” - System.TypeLoadException: Could not load type 'x' from assembly 'y' Iron Python System.TypeLoadException:无法从程序集“System.Core”加载类型“System.Runtime.CompilerServices.Closure” - Iron Python System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.Closure' from assembly 'System.Core'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM