Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

It happens when POSTing back to an MVC controller. Let's say the ViewModel looks like this:

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.

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'. System.ModuleHandle.ResolveType(RuntimeModule module, int typeToken, IntPtr* typeInstArgs, int typeInstCount, IntPtr* methodInstArgs, int methodInstCount, ObjectHandleOnStack type)

like image 792
Tyler Hartwig Avatar asked Nov 01 '25 18:11

Tyler Hartwig


1 Answers

After a couple hours of debugging, the solution turned out to be an incorrect using statement.

SelectListItem exists in both System.Web and Microsoft.AspNetCore.Mvc.Rendering

However, .NET Core does not support some element of this when referenced from System.Web

Simply changing the using statement to the following fixes the issue:

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.

like image 94
Tyler Hartwig Avatar answered Nov 04 '25 10:11

Tyler Hartwig