[英]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.Web
和Microsoft.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.