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?
antlersoft
14.8k4 gold badges40 silver badges56 bronze badges
asked May 9, 2016 at 15:33
user3378165
6,99618 gold badges66 silver badges108 bronze badges
-
Can you not just encode the file path?DoctorMick– DoctorMick2016年05月09日 15:40:06 +00:00Commented May 9, 2016 at 15:40
-
stackoverflow.com/questions/68881593/…spammer– spammer2024年07月04日 22:06:15 +00:00Commented Jul 4, 2024 at 22:06
2 Answers 2
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);
answered May 9, 2016 at 15:42
Crowcoder
11.6k3 gold badges39 silver badges46 bronze badges
Sign up to request clarification or add additional context in comments.
11 Comments
user3378165
Thank you, would you be able to give me an example of how to do it?
Crowcoder
I would if I weren't on my phone at the moment. Check out the mvc urlhelper . I would avoid system.web if possible.
user3378165
Thank you! 1-I tried that but i'm getting a 404 error, this is my URL: localhost:111/MySolution/api/documents/print/… ,Any idea how to solve that? 2-would you be able to explain why you would avoid system.web? Thanks so much!
Crowcoder
I don't know why you get 404. Are you sure your route is configured correctly? Did you try a normal string as a test instead of one that is a file path? System.Web is a heavy, bloated, legacy library that is not necessarily needed with MVC. If you already have a reference to it for another reason then it doesn't really matter.
Crowcoder
I guess it is still interpreting the route parameter as part of the route. See my edit for use of Base64 encoding.
|
System.Web.HTTPUtility.UrlEncode(@"//domain/documents/Pdf/1234.pdf")
answered May 9, 2016 at 16:16
Chris Steele
1,3811 gold badge9 silver badges21 bronze badges
Comments
lang-cs