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?
Show activity on this post. No. It is not correct to skip the slash. It may work modern browsers: however, that does not make it correct.
According to the W3C (and they are the official source on these things), a space character in the query string (and in the query string only) may be encoded as either " %20 " or " + ".
Letters ( A – Z and a – z ), numbers ( 0 – 9 ) and the characters ' ~ ',' - ',' . ' and ' _ ' are left as-is. + is encoded by %2B. All other characters are encoded as a %HH hexadecimal representation with any non-ASCII characters first encoded as UTF-8 (or other specified encoding)
The %27 is ASCII for the single quote ( ' ) and that is a red flag for someone trying to perform SQL injection via the query string to your application's data access layer logic.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With