Is it possible to override MultipartFormDataStreamProvider so that is doesn’t save uploads to the file system?

You could override MultipartFormDataStreamProvider’s GetStream method to return a stream which is not a file stream but your AWS stream, but there are some issues doing so(which I will not elaborate here). Instead you could create a provider deriving from the abstract base class MultipartStreamProvider. Following sample is heavily based on the actual source code … Read more

How to Upload Image Via WebApi

Here I have described the whole process to upload the image in web api [Route(“user/PostUserImage”)] public async Task<HttpResponseMessage> PostUserImage() { Dictionary<string, object> dict = new Dictionary<string, object>(); try { var httpRequest = HttpContext.Current.Request; foreach (string file in httpRequest.Files) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); var postedFile = httpRequest.Files[file]; if (postedFile != null && postedFile.ContentLength > 0) … Read more

Discovering Generic Controllers in ASP.NET Core

What happens by default During the controller discovery process, your open generic Controller<T> class will be among the candidate types. But the default implementation of the IApplicationFeatureProvider<ControllerFeature> interface, DefaultControllerTypeProvider, will eliminate your Controller<T> because it rules out any class with open generic parameters. Why overriding IsController() doesn’t work Replacing the default implementation of the IApplicationFeatureProvider<ControllerFeature> … Read more

DelegatingHandler for response in WebApi

Yes. You can do that in the continuation task. I explain it here. For example, this code (from the blog above) traces request URI and adds a dummy header to response. public class DummyHandler : DelegatingHandler { protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { // work on the request Trace.WriteLine(request.RequestUri.ToString()); var response = … Read more

how to return json error msg in asp.net web api?

A few points: If all you’re looking to do is return an error response containing a simple error message, Web API provides a CreateErrorResponse method for that. So you can simply do: return Request.CreateErrorResponse(HttpStatusCode.BadRequest, “An error just happened”); This will result in the following HTTP response (other headers omitted for brevity): HTTP/1.1 400 Bad Request … Read more

How do I resolve Web API controllers using Autofac in a mixed Web API and MVC application?

I would suggest the following to make this work in your application with both MVC and WebApi. First your project will need to have references to the following Autofac Autofac.WebApi Autofac.Mvc5 (change the number at the end to match your aspnet mvc version) Then in your Autofac registration you would need the following which will … Read more

Web API Queryable – how to apply AutoMapper?

Use the AutoMapper’s Queryable Extensions. First, define the mapping. // Old AutoMapper API // Mapper.CreateMap<Person, PersonDto>(); // Current AutoMapper API Mapper.Initialize(cfg => cfg.CreateMap<Person, PersonDto>() ); Then you can use something like this: [EnableQuery] public IQueryable<PersonDto> Get() { // Old AutoMapper API // return this.dbContext.Persons.Project().To<PersonDto>(); // New AutoMapper API return this.dbContext.Persons.ProjectTo<PersonDto>(); } Edit 04/2019: Updated to … Read more