Return content with IHttpActionResult for non-OK response
You can use this: return Content(HttpStatusCode.BadRequest, “Any object”);
You can use this: return Content(HttpStatusCode.BadRequest, “Any object”);
Your basic difficulty here is that JSON has two types of container: an object, and an array. From the standard: An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma). An object is an unordered set of name/value … Read more
There is no support in the current latest stable release of Web API (from August 2012). So if all you want to use is Web API RTM, you would have to implement the whole plumbing yourself. With that said, OData prerelease package supports partial updates very nicely through the new Delta<T> object. Currently the Microsoft.AspNet.WebApi.OData … Read more
You can implement your own ValidationAttribute. Perhaps something like this: public class RequireWhenCategoryAttribute : ValidationAttribute { protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var employee = (EmployeeModel) validationContext.ObjectInstance; if (employee.CategoryId == 1) return ValidationResult.Success; var emailStr = value as string; return string.IsNullOrWhiteSpace(emailStr) ? new ValidationResult(“Value is required.”) : ValidationResult.Success; } } public sealed class … Read more
Due to its nature the AuthoriseAttribute looks like it is called in the pipeline before the model binders and parameter bindings have run. You also run into issues when you access the Request.Content and read from it… this can only be done once and if you are going to try it in your auth attribute … Read more
HttpContext.Current gets the current context by Thread (I looked into the implementation directly). It would be more correct to say that HttpContext is applied to a thread; or a thread “enters” the HttpContext. Using HttpContext.Current inside of async Task is not possible, because it can run on another Thread. Not at all; the default behavior … Read more
In the WebApiConfig: config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings {NullValueHandling = NullValueHandling.Ignore}; Or, if you want more control, you can replace entire formatter: var jsonformatter = new JsonMediaTypeFormatter { SerializerSettings = { NullValueHandling = NullValueHandling.Ignore } }; config.Formatters.RemoveAt(0); config.Formatters.Insert(0, jsonformatter);
You can take a look at the following docs tutorial: Call a Web API From a .NET Client But as an answer, here I will share a quick and short a step by step guide about how to call and consume web API in Windows forms: Install Package – Install the Microsoft.AspNet.WebApi.Client NuGet package (Web … Read more
Option 1 is to switch to using XmlSerializer in GlobalConfiguration: config.Formatters.XmlFormatter.UseXmlSerializer = true; Option 2 is to decorate your models with [DataContract(Namespace=””)] (and if you do so, you’d need to decorate the members with [DataMember] attributes).
The cleanest option I’ve found is adding your own simple InputFormatter: public class RawJsonBodyInputFormatter : InputFormatter { public RawJsonBodyInputFormatter() { this.SupportedMediaTypes.Add(“application/json”); } public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context) { var request = context.HttpContext.Request; using (var reader = new StreamReader(request.Body)) { var content = await reader.ReadToEndAsync(); return await InputFormatterResult.SuccessAsync(content); } } protected override bool CanReadType(Type type) … Read more