这是我学习实现从网页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 }
);
您是否尝试过通过'http://本地主机:49932/API/simplefiles/1.zip /' –
看看这个线程,它描述了这个问题:http://stackoverflow.com/questions/429963/the-resource-cannot-be-found-error-when-there-is-a-dot-at -the-结束的-UR –