How to set up a Web API controller for multipart/form-data

I normally use the HttpPostedFileBase parameter only in Mvc Controllers. When dealing with ApiControllers try checking the HttpContext.Current.Request.Files property for incoming files instead: [HttpPost] public string UploadFile() { var file = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine( … Read more

Using angularjs filter in input element

In short: if you want your data to have a different representation in the view and in the model, you will need a directive, which you can think of as a two-way filter. Your directive would look something like angular.module(‘myApp’).directive(‘myDirective’, function() { return { require: ‘ngModel’, link: function(scope, element, attrs, ngModelController) { ngModelController.$parsers.push(function(data) { //convert … Read more

ASP.NET Web API OperationCanceledException when browser cancels the request

This is a bug in ASP.NET Web API 2 and unfortunately, I don’t think there’s a workaround that will always succeed. We filed a bug to fix it on our side. Ultimately, the problem is that we return a cancelled task to ASP.NET in this case, and ASP.NET treats a cancelled task like an unhandled … Read more

ASP.NET Core MVC : How to get raw JSON bound to a string without a type?

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

Entity framework self referencing loop detected [duplicate]

Well the correct answer for the default Json formater based on Json.net is to set ReferenceLoopHandling to Ignore. Just add this to the Application_Start in Global.asax: HttpConfiguration config = GlobalConfiguration.Configuration; config.Formatters.JsonFormatter .SerializerSettings .ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; This is the correct way. It will ignore the reference pointing back to the object. Other responses focused in changing … Read more