Showing posts with label ASP.NET Web API. Show all posts
Showing posts with label ASP.NET Web API. Show all posts

Thursday, March 20, 2014

Multiple types were found that match the controller named 'MyController' in the Web API

This is a really dumb error. It happened to me after renaming some projects in the solution, and the way to fix it is deleting the files in the bin directory and rebuilding the code. Thanks stackoverflow!

Monday, March 17, 2014

Invoking the ASP.NET Web API from a client in .NET 3.5

It's actually pretty simple. I followed this post, but basically you just have to do the following.

http://www.thepicketts.org/2012/11/how-to-access-webapi-from-a-net-3-5-client-in-c/

public static object postWebApi(object data, Uri webApiUrl) {
    // Create a WebClient to POST the request
    WebClient client = new WebClient();

    // Set the header so it knows we are sending JSON
    client.Headers[HttpRequestHeader.ContentType] = "application/json";

    // Serialise the data we are sending in to JSON
    string serialisedData = JsonConvert.SerializeObject(data);

    // Make the request
    var response = client.UploadString(webApiUrl, serialisedData);

    // Deserialise the response into a GUID
    return JsonConvert.DeserializeObject(response);
}