1

When I start an Asp.net MVC4 website in visual studio 2012. I was stuck with an exception said :

Could not load type 'System.Net.Http.Formatting.DefaultContentNegotiator' 
from assembly 'System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=31bf3856ad364e35'.

On the code :

routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = System.Web.Http.RouteParameter.Optional }
            );

the Whole code looks like :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Http;


namespace Accela.Apps.Diagnostics.Portal
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = System.Web.Http.RouteParameter.Optional }
            );


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

Please help get me out of there.thanks.

Joe.wang
  • 11,537
  • 25
  • 103
  • 180

2 Answers2

2

Looks like you are having old version of the DLL. What you can do is, remove the existing reference to 'System.Net.Http.Formatting' from your project and try installing this

Siddaraju
  • 96
  • 5
  • thanks ,Did you attention this line `Legacy package, System.Net.Http.Formatting is now included in the 'Microsoft.AspNet.WebApi.Client'package` in the page you provided? – Joe.wang Dec 17 '12 at 09:34
  • The web site I created is a `Web Api` project. `System.Net.Http.Formatting` is already include in it . Should I remove the existing one ? thanks. – Joe.wang Dec 17 '12 at 09:39
  • 1
    Hey Joe, I understand. Can you right click on 'System.Net.Http.Formatting' under project references and do 'view in Object Browser' then you can check 'DefaultContentNegotiator' is available. – Siddaraju Dec 17 '12 at 09:45
  • Hi Sids, After I checked it , I found the public class `DefaultContentNegotiator` is already a Member of `System.Net.Http.Formatting` namespace. thanks. – Joe.wang Dec 17 '12 at 09:49
  • Hi Sids, The version of the dll cause the problem. I had updated it. thanks. – Joe.wang Dec 17 '12 at 10:05
  • 1
    [See this](http://stackoverflow.com/questions/9431975/could-not-load-file-or-assembly-system-net-http-version-2-0-0-0-in-mvc4-web-ap) this should give you some idea – Siddaraju Dec 17 '12 at 10:08
0

Should that optional parameter not just be

UrlParameter.Optional 

as in

routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = UrlParameter.Optional  }
            );

instead of System.Web.Http.RouteParameter.Optional

dove
  • 20,469
  • 14
  • 82
  • 108