Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use <a href="http://msdn.microsoft.com/en-us/library/system.uri.trycreate.aspx" rel="nofollow noreferrer">Uri.TryCreate</a> to create a new Uri object only if your url string is a valid URL. If the string is not a valid URL, TryCreate returns false. </p> <pre><code>string myString = "http://someUrl"; Uri myUri; if (Uri.TryCreate(myString, UriKind.RelativeOrAbsolute, out myUri)) { //use the uri here } </code></pre> <p><strong>UPDATE</strong></p> <p>TryCreate or the Uri constructor will happily accept strings that may appear invalid, eg "Host: www.stackoverflow.com","Host:%20www.stackoverflow.com" or "chrome:about". In fact, these are perfectly valid URIs that specify a custom scheme instead of "http". </p> <p>The documentation of the <a href="http://msdn.microsoft.com/en-us/library/system.uri.scheme.aspx" rel="nofollow noreferrer">Uri.Scheme</a> property provides more examples like "gopher:" (anyone remember this?), "news", "mailto", "uuid".</p> <p>An application can register itself as a custom protocol handler as described in <a href="http://msdn.microsoft.com/en-us/library/aa767914.aspx" rel="nofollow noreferrer">MSDN</a> or other SO questions, eg <a href="https://stackoverflow.com/questions/80650/how-do-i-register-a-custom-url-protocol-in-windows">How do I register a custom URL protocol in Windows?</a></p> <p>TryCreate doesn't provide a way to restrict itself to specific schemes. The code needs to check the Uri.Scheme property to ensure it contains an acceptable value</p> <p><strong>UPDATE 2</strong></p> <p>Passing a weird string like <code>"&gt;&lt;/script&gt;&lt;script&gt;alert(9)&lt;/script&gt;</code> will return <code>true</code> and construct a relative Uri object. Calling <a href="http://msdn.microsoft.com/en-us/library/system.uri.iswellformedoriginalstring%28v=vs.110%29.aspx" rel="nofollow noreferrer">Uri.IsWellFormedOriginalString</a> will return false though. So you probably need to call <code>IsWellFormedOriginalString</code> if you want to ensure that relative Uris are well formed.</p> <p>On the other hand, calling <code>TryCreate</code> with <code>UriKind.Absolute</code> will return false in this case.</p> <p>Interestingly, Uri.IsWellFormedUriString calls TryCreate internally and then returns the value of <code>IsWellFormedOriginalString</code> if a relative Uri was created.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload