简体   繁体   English

查询字符串参数中的斜杠?

[英]Slashes in a query string parameter?

How can I send a file path as a query string parameter? 如何将文件路径作为查询字符串参数发送?

This is my string parameter: 这是我的字符串参数:

//domain/documents/Pdf/1234.pdf //domain/documents/Pdf/1234.pdf

I have tried that: 我试过了:

    [HttpPost]
    [Route("documents/print/{filePath*}")]
    public string PrintDocuments([FromBody] string[] docs,string filePath)
    {
       .....
    }

But this is not working, I guess because of the double slashes in the beginning of the parameter. 但这不起作用,我想是因为参数开头的双斜线。

Any idea? 任何的想法?

If, like you say, that entire string is the parameter, and not a route, you will need to URL encode it. 如果像你说的那样,整个字符串是参数而不是路由,则需要对其进行URL编码。 You should always do this anyway: 无论如何你应该总是这样做:

System.Net.WebUtility.UrlEncode(<your string>);
// %2F%2Fdomain%2Fdocuments%2FPdf%2F1234.pdf

Update 更新

Since that is not working, I would suggest you Base64 encode it instead of URL encode it: 由于这不起作用,我建议您使用Base64编码而不是URL编码:

var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(<your string>);
var encodedFilePath = System.Convert.ToBase64String(plainTextBytes);

..and in your controller decode it: ..并在你的控制器解码它:

byte[] data = Convert.FromBase64String(filepath);
string decodedString = Encoding.UTF8.GetString(data);
System.Web.HTTPUtility.UrlEncode(@"//domain/documents/Pdf/1234.pdf")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM