Slashes in a query string parameter?

陌路散爱 提交于 2020-01-03 07:25:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!