c#WebAPI将文档路径作为参数传递

时间:2014-09-17 17:09:40

标签: c# asp.net-web-api

在测试下面的代码时,请按照以下方式进行测试:

http://localhost:49494/api/DownloadFile/q%3a%5cLetters%5cOnlineReminder.pdf

导致此异常:
从客户端检测到潜在危险的Request.Path值

有这样的话:

http://localhost:49494/api/DownloadFile?q%3a%5cLetters%5cOnlineReminder.pdf

函数中文档(FullPath)的路径为空

以下是代码:

[HttpGet]
[Route("api/DownloadFile/{*FullPath}")]
public IHttpActionResult DownloadFile(string FullPath)
{
    if (FullPath == null)
        return NotFound();

    System.IO.MemoryStream Stream = _ArchiveRepository.GetDownloadStream(FullPath);

    return new MemoryStreamResult(Stream, "application/octet-stream");
}

我也编写了一个MemoryStreamresult。

1 个答案:

答案 0 :(得分:2)

Scott Hanselman在http://www.hanselman.com/blog/ExperimentsInWackinessAllowingPercentsAnglebracketsAndOtherNaughtyThingsInTheASPNETIISRequestURL.aspx写过这篇文章。您可能需要检查web.config中节点的requestPathInvalidCharacters属性。默认情况下,符号%是无效字符

也许您应该尝试使用此表单来请求yourlocal / api / DownloadFile /?FullPath = q%3a%5cLetters%5cOnlineReminder.pdf,否则从requestPathInvalidCharacters中删除字符