How to test my download files web API in c#

1.7k views Asked by At

I want to download files from web service . i 've tried to do this but i can't tested it in advanced rest client . here is my code

public static HttpResponseMessage FileAsAttachment(string path, string filename)
        {
            if (File.Exists(path))
            {

                HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                var stream = new FileStream(path, FileMode.Open);
                result.Content = new StreamContent(stream);
                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                result.Content.Headers.ContentDisposition.FileName = filename;
                return result;
            }
            return new HttpResponseMessage(HttpStatusCode.NotFound);
        }

I always have response " "$id": "1" "Message": "No HTTP resource was found that matches the request URI 'http://localhost:36690/api/Data/FileAsAttachment?path=F:\&filename=claims.jpg'." "MessageDetail": "No action was found on the controller 'Data' that matches the name 'FileAsAttachment'." My controller name is data

1

There are 1 answers

6
AudioBubble On

Action method should not be static. When I remove static from your code it is working.

Why there is a need for declaring it as static method?

Please have a look on the below links.

Link1 Link2

Update: After Comment, try decorating method with [HttpGet]

 [HttpGet]
 public HttpResponseMessage FileAsAttachment(string path, string filename)