15

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
2

2 Answers 2

14

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
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you, would you be able to give me an example of how to do it?
I would if I weren't on my phone at the moment. Check out the mvc urlhelper . I would avoid system.web if possible.
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!
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.
I guess it is still interpreting the route parameter as part of the route. See my edit for use of Base64 encoding.
|
0
System.Web.HTTPUtility.UrlEncode(@"//domain/documents/Pdf/1234.pdf")
answered May 9, 2016 at 16:16

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.