Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add scheme to URL if needed

Tags:

c#

.net

uri

To create a Uri from a string you can do this:

Uri u = new Uri("example.com"); 

But the problem is if the string (like the one above) doesn't contain the protocol you will get an exception: "Invalid URI: The format of the URI could not be determined."

To avoid the exception you should secure the string includes a protocol, like below:

Uri u = new Uri("http://example.com"); 

But if you take the url as input, how can you add the protocol if it's missing?
I mean apart from some IndexOf/Substring manipulation?

Something elegant and fast?

like image 975
Max Favilli Avatar asked Mar 13 '11 13:03

Max Favilli


People also ask

Is scheme mandatory in URL?

This is a valid URL. It's called a "network-path reference" as defined in RFC 3986. When you don't specify a scheme/protocol, it will fall back to the current scheme. So if you are viewing a page via https:// all network path references will also use https.

What is custom URI scheme?

Overview. Custom URL schemes provide a way to reference resources inside your app. Users tapping a custom URL in an email, for example, launch your app in a specified context. Other apps can also trigger your app to launch with specific context data; for example, a photo library app might display a specified image.

What is the meaning of the URL scheme?

2.2.4 Definition of URL schemes not associated with data resources Most URL schemes locate Internet resources that correspond to data objects that can be retrieved or modified.

What is an application URL?

An app: URL is a [ URL ] that can be used by a packaged application to address resources within its container (e.g., a . zip file).


Video Answer


2 Answers

You could also use UriBuilder:

public static Uri GetUri(this string s) {     return new UriBuilder(s).Uri; } 

Remarks from MSDN:

This constructor initializes a new instance of the UriBuilder class with the Fragment, Host, Path, Port, Query, Scheme, and Uri properties set as specified in uri.

If uri does not specify a scheme, the scheme defaults to "http:".

like image 101
as-cii Avatar answered Sep 19 '22 18:09

as-cii


If you just want to add the scheme, without validating the URL, the fastest/easiest way is to use string lookups, eg:

string url = "mydomain.com"; if (!url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)) url = "http://" + url; 

A better approach would be to use Uri to also validate the URL using the TryCreate method:

string url = "mydomain.com"; Uri uri; if ((Uri.TryCreate(url, UriKind.Absolute, out uri) || Uri.TryCreate("http://" + url, UriKind.Absolute, out uri)) &&     (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps)) {     // Use validated URI here } 

As @JanDavidNarkiewicz pointed out in the comments, validating the Scheme is necessary to guard against invalid schemes when a port is specified without scheme, e.g. mydomain.com:80.

like image 23
Ronald Avatar answered Sep 18 '22 18:09

Ronald