lundi 1 janvier 2018

Web API 2 - Error: "No type was found that matches the controller named 'resize'."

I am trying to just do a simple file upload API using Web API.

Here is the Controller:

[RoutePrefix("api/resize")]
public class ResizeController : ApiController
{

    [HttpPost, Route("api/resize/preserveAspectRatio")]
    public async Task<IHttpActionResult> resizePreserveAspectRatio()
    {
        if (!Request.Content.IsMimeMultipartContent())
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

        int maxWidth = 100;
        int maxHeight = 100;

        var provider = new MultipartMemoryStreamProvider();
        await Request.Content.ReadAsMultipartAsync(provider);
        foreach (var file in provider.Contents)
        {
            var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
            var buffer = await file.ReadAsByteArrayAsync();
            //Do whatever you want with filename and its binaray data.


        }

        return Ok();
    }


}

This is my WebApiConfig:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

When I POST a file with PostMan, here is the error I get:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:26303/api/resize/preserveAspectRatio'.",
    "MessageDetail": "No type was found that matches the controller named 'resize'."
}

This is not a dupe - was not able to find another article that addresses this specific combination.

Any help is greatly appreciated!




Aucun commentaire:

Enregistrer un commentaire