8

I downloaded a sample code for webapi and I am getting this exception I opened this sample using Visual Studio 2012 Ultimate Version and I have latest verion of newtonsoft installed.I am getting this error when I run this application.Its compiling successfully. Any idea why this error and is there a way to resolve this exception.

System.MissingMethodException was unhandled by user code
  HResult=-2146233069
  Message=Method not found: 'Void Newtonsoft.Json.Serialization.DefaultContractResolver.set_IgnoreSerializableAttribute(Boolean)'.
  Source=System.Net.Http.Formatting
  StackTrace:
       at System.Net.Http.Formatting.JsonContractResolver..ctor(MediaTypeFormatter formatter)
       at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
       at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
       at System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor()
       at System.Web.Http.HttpConfiguration.DefaultFormatters()
       at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)
       at System.Web.Http.GlobalConfiguration.<.cctor>b__0()
       at System.Lazy`1.CreateValue()
       at System.Lazy`1.LazyInitValue()
       at System.Lazy`1.get_Value()
       at System.Web.Http.GlobalConfiguration.get_Configuration()
       at System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults, Object constraints, HttpMessageHandler handler)
       at System.Web.Http.RouteCollectionExtensions.MapHttpRoute(RouteCollection routes, String name, String routeTemplate, Object defaults)
       at WebAPIRc.RouteConfig.RegisterRoutes(RouteCollection routes) in c:\Users\viemon\Downloads\WebAPIRc\WebAPIRc\WebAPIRc\App_Start\RouteConfig.cs:line 17
       at WebAPIRc.WebApiApplication.Application_Start() in c:\Users\viemon\Downloads\WebAPIRc\WebAPIRc\WebAPIRc\Global.asax.cs:line 36
  InnerException: 

Here is the code where it fails

public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
          //Exception error start
            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
          //exception error end

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

I came to know that I need to turn on "Include prerelease" for NuGet from this post, but how do I turn on how to turn on the "Include Prerelease" for NuGet ?

Community
  • 1
  • 1
Steve
  • 1,471
  • 7
  • 19
  • 32

3 Answers3

10

how do I turn on how to turn on the "Include Prerelease" for NuGet ?

To install Json.NET with "Include Prerelease", run the following command in the Package Manager Console

Install-Package Newtonsoft.Json –IncludePrerelease
Damith
  • 62,401
  • 13
  • 102
  • 153
2

Another cause of this can be if you have an older version in your GAC. For me the version was in the .NET 4.0 GAC removing this resolved the issue

PatrickWalker
  • 550
  • 5
  • 19
  • Can you give more details about this? – dr.Crow Oct 29 '15 at 02:55
  • Hey, I'm casting my mind back but I think I had a version in my .NET4 Gac which can be identified here http://stackoverflow.com/questions/6925043/where-is-gacutil-for-net-framework-4-0-in-windows-7 and I had to remove that so the runtime would pick up the newer version https://msdn.microsoft.com/en-us/library/zykhfde0(v=vs.110).aspx I don't know why that version was in my GAC originally. Hope that helps – PatrickWalker Oct 29 '15 at 12:25
1

In My Case You I just remove the following line in RouteConfig (it was already obliterated).

  //Exception error start
    routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
  //exception error end

In VS2012, this should be enough.

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

You may also need to verify your controller. Also remove some Class reference in RouteConfig, if you are not sure if what they were for. In My Case I just retain the these two:

using System.Web.Mvc;
using System.Web.Routing;

Another solution is if you want and have a time to figure out what causes the conflict, then you may set your Project Build Output verbosity to Detailed (Go to Tools-> Options -> Project and Solutions -> Build and Run -> MSBuild project build output verbosity) and upon build check your logs.

dr.Crow
  • 1,493
  • 14
  • 17
  • The accepted answer did not work for me but removing this from the RouteConfig did. But do you have any documents stating that removing this will not cause any issues down the line? – Chad Sep 26 '16 at 14:02