查询字符串参数中的斜线? [英] Slashes in a query string parameter?
问题描述
如何将文件路径作为查询字符串参数发送?
这是我的字符串参数:
<块引用>//域/文档/PDF/1234.pdf
我已经试过了:
[HttpPost][Route("documents/print/{filePath*}")]public string PrintDocuments([FromBody] string[] docs,string filePath){.....}
但这不起作用,我猜是因为参数开头的双斜杠.
有什么想法吗?
如果,就像你说的,整个字符串是参数,而不是路由,你需要对它进行 URL 编码.无论如何,您应该始终这样做:
System.Net.WebUtility.UrlEncode(<你的字符串>);//%2F%2Fdomain%2Fdocuments%2Fpdf%2F1234.pdf
<块引用>
更新
由于这不起作用,我建议您对其进行 Base64 编码而不是 URL 编码:
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(<你的字符串>);var encodingFilePath = System.Convert.ToBase64String(plainTextBytes);
..并在您的控制器中对其进行解码:
byte[] data = Convert.FromBase64String(filepath);字符串解码字符串 = Encoding.UTF8.GetString(data);
How can I send a file path as a query string parameter?
This is my string parameter:
//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. 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:
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);
这篇关于查询字符串参数中的斜线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!