skip to Main Content

Environment: Windows Server 2012 R2, IIS 8.5.

I built an API that works wonders on our dev environment. It reads a text-file, then serializes the contents into JSON and returns that. I did a File System deploy to our server. I created a website pointing at the folder, and figured that’d do the trick nicely. I made that website the default.

My app contains routing to handle two different routes:

/api/twitter/{date}
/api/twitter/{date}/{id}

Either route is supposed to spit out a JSON string.

Going on the server, entering localhost brings up the localhost screen. localhost/api yields a 404.0 not found, which sounds okay, seeing there’s no route to handle that. localhost/api/twitter/20160201, which should work, doesn’t really do anything. It instead appears to be wanting to download a file called 20160201. Clicking open does nothing, and clicking save yields an empty file that never finishes. I’m suspecting JSON.NET is not properly being called, or that the server might somehow is not expecting JSON to be returned to the breowser.

In the error message, I can see that the Physical Path is at least pointing to the correct path.

Here’s the controller part:

// Default route: 
[Route("api/twitter/{date}")]
public Twitter GetTweets(int date) {
    return LoadJson(date);
}

// Additional route for date + id for incremental loading
[Route("api/twitter/{date}/{id}")]
[HttpGet]
public Twitter GetTweets(int date, Int64 id) {
    return GetTweetsIncremental(date, id);
}

// more code

WebApiConfig

GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new RequestHeaderMapping(“Accept”, “text/html”, StringComparison.InvariantCultureIgnoreCase, true, “application/json”));
// Web API configuration and services

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{date}",
        defaults: new { date = RouteParameter.Optional}
    );

Global.asax contains this snippet to have the api spit out JSON rather than XML:

    // Remove the XML formatter
   //  GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
    var serializerSettings =
      GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings;
    var contractResolver =
      (DefaultContractResolver)serializerSettings.ContractResolver;
    contractResolver.IgnoreSerializableAttribute = true;

It relies on Newtonsoft.Json, which I see as a reference, and which I’m using in the code. I also marked that as a “Copy Local = true” reference, to ensure the Publish would push the DLLs to the server.

In my Web.config, I see the following assembly bindings (note the lack of Newtonsoft.json):

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
  </dependentAssembly>
</assemblyBinding>

I’m pretty stumped at the moment as to what’s happening. I initially suspected it was the 404 error, which had me try different things, but I reached the conclusion that since there’s happening when I access the actual routes, I’ve been barking up the wrong tree. I just cannot figure out what’s going wrong.

Is it the missing reference to the newtonsoft assembly? If so, how’d i go about getting that properly registered?

Any ideas?

Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    So this turned out to be due to the Server we deployed the API to not being defined as an Application Server. That seems to prevent the JSON from somehow properly being parsed. Once we added that role, things started working.


  2. 404 means, to me, the server is not capable of identifying the right controller for the job. ” It instead appears to be wanting to download a file called 20160201″.

    Are you really sure ASP.Net is configured on the server, enabled on the website, and that the Application Pool is configured to process the application? AFAICT, Json.Net and the code don’t have nothing to do with your problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search