UriBuilder.Query

I found UriBuilder weird behavior in .NET

Senario 1:

Dim uri As New UriBuilder("http://www.test/login.aspx") uri.Query = "?test=Test" Dim url As String = uri.ToString() 

After running this code, the url line contains "http: //www.test/login.aspx ?? test = Test"

The solution was to not add ?.

Senario 2:

  Dim uri As New UriBuilder("http://www.test/login.aspx?test=123") uri.Query += "&abc=Test" Dim url As String = uri.ToString() 

After this code, do we have two again? "HTTP: //www.test: 80 / login.aspx ?? test = 123 & abc = Test."

So am I doing something wrong when using the uri builder?

+6
source share
2 answers

According to the MSDN docs comment for this class, this error appears if you set the request property several times.

Just by looking at the decompiler, does the Query installer always add a lead ? if the setpoint is not empty.

+6
source

The following example sets the Query property.

  UriBuilder baseUri = new UriBuilder("http://www.contoso.com/default.aspx?Param1=7890"); string queryToAppend = "param2=1234"; if (baseUri.Query != null && baseUri.Query.Length > 1) baseUri.Query = baseUri.Query.Substring(1) + "&" + queryToAppend; else baseUri.Query = queryToAppend; 

First char ? not needed.

Additional information: http://msdn.microsoft.com/en-us/library/system.uribuilder.query.aspx

+7
source

Source: https://habr.com/ru/post/918361/


All Articles