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

问题描述 投票:-1回答:1

在开发ASP.NET Core MVC项目的过程中,我多次遇到此异常。

当POST回MVC控制器时会发生这种情况。假设ViewModel看起来像这样:

using System.Web;

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

此视图的标题包含此内容

@model MyNamespace.MyViewModel

如前所述,在POST返回时(假设View中有一个表单和一个回发),抛出此异常。

处理请求时发生未处理的异常。 TypeLoadException:无法从程序集“System.Web,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”加载类型“System.Web.PreApplicationStartMethodAttribute”。 System.ModuleHandle.ResolveType(RuntimeModule模块,int typeToken,IntPtr * typeInstArgs,int typeInstCount,IntPtr * methodInstArgs,int methodInstCount,ObjectHandleOnStack类型)

c# exception .net-core asp.net-core-mvc
1个回答
-2
投票

经过几个小时的调试,该解决方案被证明是一个不正确的使用声明。

SelectListItem存在于System.WebMicrosoft.AspNetCore.Mvc.Rendering

但是,当从System.Web引用时,.NET Core不支持某些元素

只需将using语句更改为以下内容即可解决问题:

using Microsoft.AspNetCore.Mvc.Rendering;

这可能与其他类型一起发生,因此如果您在ASP.NET Core MVC中遇到奇怪的错误,最好检查System.Web的using语句。

© www.soinside.com 2019 - 2024. All rights reserved.