Selectively use default JSON converter

You could add a dummy converter to the properties in question that does nothing: public class NoConverter : JsonConverter { public override bool CanConvert(Type objectType) { // Note – not called when attached directly via [JsonConverter(typeof(NoConverter))] throw new NotImplementedException(); } public override bool CanRead { get { return false; } } public override object ReadJson(JsonReader … Read more

Asp.Net Web API Error: The ‘ObjectContent`1’ type failed to serialize the response body for content type ‘application/xml; charset=utf-8’

I would suggest Disable Proxy Creation only in the place where you don’t need or is causing you trouble. You don’t have to disable it globally you can just disable the current DB context via code… [HttpGet] [WithDbContextApi] public HttpResponseMessage Get(int take = 10, int skip = 0) { CurrentDbContext.Configuration.ProxyCreationEnabled = false; var lista = … Read more

ASP.NET Web API 2: How do I log in with external authentication services?

I had the same problem today and found the following solution: At first get all available providers GET /api/Account/ExternalLogins?returnUrl=%2F&generateState=true The response message is a list in json format [{“name”:”Facebook”, “url”:”/api/Account/ExternalLogin?provider=Facebook&response_type=token&client_id=self&redirect_uri=http%3A%2F%2Flocalhost%3A15359%2F&state=QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1″, “state”:”QotufgXRptkAfJvcthIOWBnGZydgVkZWsx8YrQepeDk1″}] Now send a GET request to the url of the provider you want to use. You will be redirected to the login page of … Read more

Strings sent through Web API’s gets wrapped in quotes

The quotes and backslashes are being added at each “proxy” API as the JSON string is re-serialized for each response, not when the response is received. In your proxy API, presumably you are doing something like this (error handling omitted for brevity): [HttpGet] public async Task<HttpResponseMessage> GetWidget(int id) { HttpClient client = new HttpClient(); string … Read more

Routing with multiple Get methods in ASP.NET Web API

From here Routing in Asp.net Mvc 4 and Web Api Darin Dimitrov has posted a very good answer which is working for me. It says… You could have a couple of routes: public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: “ApiById”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional }, … Read more

ASP.NET WebApi: how to perform a multipart post with file upload using WebApi HttpClient

After much trial and error, here’s code that actually works: using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent()) { var values = new[] { new KeyValuePair<string, string>(“Foo”, “Bar”), new KeyValuePair<string, string>(“More”, “Less”), }; foreach (var keyValuePair in values) { content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key); } var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(fileName)); fileContent.Headers.ContentDisposition = … Read more

Query string not working while using attribute routing

I was facing the same issue of ‘How to include search parameters as a query string?’, while I was trying to build a web api for my current project. After googling, the following is working fine for me: Api controller action: [HttpGet, Route(“search/{categoryid=categoryid}/{ordercode=ordercode}”)] public Task<IHttpActionResult> GetProducts(string categoryId, string orderCode) { } The url I tried … Read more

Token Based Authentication in ASP.NET Core (refreshed)

Working from Matt Dekrey’s fabulous answer, I’ve created a fully working example of token-based authentication, working against ASP.NET Core (1.0.1). You can find the full code in this repository on GitHub (alternative branches for 1.0.0-rc1, beta8, beta7), but in brief, the important steps are: Generate a key for your application In my example, I generate … Read more

web-api POST body object always null

FromBody is a strange attribute in that the input POST values need to be in a specific format for the parameter to be non-null, when it is not a primitive type. (student here) Try your request with {“name”:”John Doe”, “age”:18, “country”:”United States of America”} as the json. Remove the [FromBody] attribute and try the solution. … Read more