【问题标题】:c# WebAPI pass document path as parameterc# WebAPI将文档路径作为参数传递
【发布时间】:2014-09-18 01:33:43
【问题描述】:

在下面测试我的代码时,如下所示:

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。

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 实际上不得不使用带有FullPath的查询字符串作为参数。 .../DownloadFile?FullPath=q%3a%5cLetters%5cOnlineReminder.pdf
    猜你喜欢
    • 2013-01-25
    • 1970-01-01
    • 2021-12-02
    • 2013-02-26
    • 2014-05-15
    • 1970-01-01
    • 2020-07-27
    • 2014-08-12
    • 1970-01-01
    相关资源
    最近更新 更多