0

Is this expected? For example: https://google.com/hello?w=orld#hi

Uri.PathAndQuery would result:

/hello?w=orld

Fully excluding the # bit even though I require it. What should I do here? Should I manually do a PathAndQuery like operation perhaps:

string fullUri = Uri.ToString();
Uri.Host + "/" + fullUri .Substring(fullUri.indexOf(Uri.Host)+Uri.Host.Length)

Essentially it compiles google.com, /, hello?w=orld#hi which would be an expected result

Im retrieving this specifically for a stream write request related operation:

{0} {1} HTTP/1.1\r\n {0} = Method {1} = pathandquery
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Ma Dude
  • 477
  • 1
  • 5
  • 17
  • P.S. But Not Related by Much: Why would I need to actually use a style like /path?q=uery when using the full url works as much? Is there some kind of HTTP1.1 specific thing im missing or some compatability issue if I use the full url? – Ma Dude Sep 29 '18 at 08:06
  • The # is the [Fragment](https://learn.microsoft.com/en-us/dotnet/api/system.uri.fragment?redirectedfrom=MSDN&view=netframework-4.7.2) separator in an Uri. It is not part of the Path nor of the Query which explains your result. That is by design. – rene Sep 29 '18 at 08:36

1 Answers1

2

The #hi part is called "fragment", you can access it through .Fragment. Since the property is called PathAndQuery, not PathAndQueryAndFragment, I assume this works as intended. As far as I know there is no method or property available which includes the fragment, but you can easily attach it:

var uri = new Uri("https://google.com/hello?w=orld#hi");
var pathAndQueryAndFragment = $"{uri.PathAndQuery}{uri.Fragment}";

But be aware that the fragment part is usually not submitted to the server.

likle
  • 1,717
  • 8
  • 10
  • Ok that makes sense, but I specifically need to make the request to `https://google.com/hello?w=orld#hi` (example) as the webpage im going to uses the # to check as a result. For example what im doing is im logging in, that login page (lets just say /sso) redirects me to /login#result-bademail if I were to not have the fragment, i would end up making that get request to /login only, resulting in it thinking i just normally went to /login and shows just the login form. Meaning google chrome atleast sends with the fragment? – Ma Dude Sep 30 '18 at 02:32
  • The login page might include all the content, and it just hides and shows parts of it based on the fragment using CSS/JavaScript. – likle Sep 30 '18 at 07:43
  • You probably also want to watch out for AJAX requests, since such pages often work this way in order to avoid page reloads. – likle Sep 30 '18 at 08:20
  • Right so when you meant usually not submitted you mean as in PHP e.t.c ignores the fragment not as in client programs usually dont include it right? So its fine and safe if I were to just include the Fragment? – Ma Dude Sep 30 '18 at 22:51
  • Clients don't include the fragment. You really shouldn't include it in a HTTP request. From [RFC 2396](https://tools.ietf.org/html/rfc2396#section-4.1): _it is not part of a URI, but is often used in conjunction with a URI_. And [RFC 2616](https://tools.ietf.org/html/rfc2616.html#section-5.1.2) only permitts "absoluteURI", "abs_path" and "authority" - none of those have a fragment identifier. This means your request will most likely be interpreted as invalid. – likle Sep 30 '18 at 23:42
  • but if I dont include it the request will send as "https://google.com/hello?w=orld" not "https://google.com/hello?w=orld#hi" like chrome :/ – Ma Dude Oct 01 '18 at 06:54
  • I included fragment and it seems to work fine in general, I understand its not meant to be included but in my scenario of how my requests are used and their purpose it should be OK in general. Thank you for the help – Ma Dude Oct 01 '18 at 06:55