问题
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?
回答1:
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);
回答2:
System.Web.HTTPUtility.UrlEncode(@"//domain/documents/Pdf/1234.pdf")
来源:https://stackoverflow.com/questions/37119748/slashes-in-a-query-string-parameter