2015-08-28 26 views
2

这是我学习实现从网页API下载文件中的link,我试图下载文件,这些URL如何使用URL末尾的点来调用Web API操作?

http://localhost:49932/api/simplefiles/1.zip < - 不工作时,抱怨没有找到 http://localhost:49932/api/simplefiles/1 <方法 - 能调用动作名称,但为什么?

我知道一些与“.zip”扩展名相关的URL,导致失败,但我只是没有得到它,不知道什么是错的,任何人都可以解释一下吗?

接口

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

控制器

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 } 
      ); 
+1

您是否尝试过通过'http://本地主机:49932/API/simplefiles/1.zip /' –

+0

看看这个线程,它描述了这个问题:http://stackoverflow.com/questions/429963/the-resource-cannot-be-found-error-when-there-is-a-dot-at -the-结束的-UR –

回答

1

IIS,默认情况下,BL ocks可以访问它无法识别的任何文件类型。如果URL中包含一个点(。),则IIS会使用其文件名并阻止访问。

要允许IIS网站上的URL(可能是用于MVC路由路径中的域名/电子邮件地址等)中的点,您必须进行一些更改。

快速修复

一个简单的选择是一个/追加到末尾。这告诉IIS它的文件路径而不是文件。

http://localhost:49932/api/simplefiles/1.zip变成http://localhost:49932/api/simplefiles/1.zip/

但是,这并不理想:手动键入URL的用户可能会忽略前导斜杠。

你可以告诉IIS不加入以下打扰你:

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

检查此链接:http://average-joe.info/allow-dots-in-url-iis/

相关问题