Slashes in a query string parameter?

时间:2022-11-23 18:51:59

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?

2 个解决方案

#1


7  

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:

如果像你说的那样,整个字符串是参数,而不是路由,则需要对其进行URL编码。无论如何你应该总是这样做:

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);

#2


0  

System.Web.HTTPUtility.UrlEncode(@"//domain/documents/Pdf/1234.pdf")

#1


7  

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:

如果像你说的那样,整个字符串是参数,而不是路由,则需要对其进行URL编码。无论如何你应该总是这样做:

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);

#2


0  

System.Web.HTTPUtility.UrlEncode(@"//domain/documents/Pdf/1234.pdf")