5

This is the link that i study to implement the download file from web api, i tried download file with these URL

http://localhost:49932/api/simplefiles/1.zip <--NOT WORKING, complain method not found http://localhost:49932/api/simplefiles/1 <--able to call the action name, but why?

i know something to do with the ".zip" extension putting in the URL that cause failure but i'm just not get it and not sure what's wrong, could anyone explain a little?

Interface

public interface IFileProvider  
{
    bool Exists(string name);
    FileStream Open(string name);
    long GetLength(string name);
}

Controller

public class SimpleFilesController : ApiController  
{
    public IFileProvider FileProvider { get; set; }

    public SimpleFilesController()
    {
        FileProvider = new FileProvider();
    }

    public HttpResponseMessage Get(string fileName)
    {
        if (!FileProvider.Exists(fileName))
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }

        FileStream fileStream = FileProvider.Open(fileName);
        var response = new HttpResponseMessage();
        response.Content = new StreamContent(fileStream);
        response.Content.Headers.ContentDisposition
            = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = fileName;
        response.Content.Headers.ContentType
            = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentLength 
                = FileProvider.GetLength(fileName);
        return response;
    }
}

WebAPIConfig

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{filename}",
                defaults: new { id = RouteParameter.Optional }
            );
Lukasz M
  • 5,635
  • 2
  • 22
  • 29
tsohtan
  • 830
  • 1
  • 13
  • 33
  • 1
    Have you tried `http://localhost:49932/api/simplefiles/1.zip/` – Bhushan Firake Aug 28 '15 at 09:42
  • Take a look at this thread, which describes the issue: http://stackoverflow.com/questions/429963/the-resource-cannot-be-found-error-when-there-is-a-dot-at-the-end-of-the-ur – Lukasz M Aug 28 '15 at 09:46

1 Answers1

4

IIS, by default, blocks access to any file types it doesn’t recognise. If a dot (.) is in the URL, IIS assumes its a file name and blocks access.

To allow dots in a URL on an IIS site (perhaps for domain names/email addresses/etc. in a MVC routing path) you have to make a couple of changes.

Quick Fix

One simple option is to append a / to the end. This tells IIS its a filepath and not a file.

http://localhost:49932/api/simplefiles/1.zip becomes http://localhost:49932/api/simplefiles/1.zip/.

However, this isn’t ideal: customers who type in the URL manually will probably ignore the leading slash.

You can tell IIS not to bother you by adding following :

<system.webServer>
      <modules runAllManagedModulesForAllRequests="true" />
<system.webServer>

Check this link: http://average-joe.info/allow-dots-in-url-iis/

Bhushan Firake
  • 9,338
  • 5
  • 44
  • 79