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

12,702

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.

Share:
12,702

Related videos on Youtube

Tyler Hartwig
Author by

Tyler Hartwig

I develop primarily in .NET, with a particular interest in F#.

Updated on June 04, 2022

Comments

  • Tyler Hartwig
    Tyler Hartwig almost 2 years

    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)

    • Camilo Terevinto
      Camilo Terevinto over 6 years
      System.Web => ASP.NET; Microsoft.AspNetCore => ASP.NET Core. DON'T mix them up.