Create Url using Flurl Url class and add port

489 Views Asked by At

What is the correct way to create Url and add port?

This is how I create URL but I do not find any Url method to add port.

new Url("https://www.myurl.com");

Should I just enter Port directly into the string like this?

new Url("https://www.myurl.com:9000");

2

There are 2 best solutions below

1
On

I don't think there is a special way of doing it. It all depends on how you want to read it.

var url = new Url("https://www.myurl.com:9000");

or

var url = new Url("https://www.myurl.com") {Port = 9000};

or even

var url = new Url
{
    Scheme = "https",
    Host = "www.myurl.com", 
    Port = 9000
};

should all be fine.

another way is to use string interpolation in the first example:

int port = 9000;    
var url = new Url($"https://www.myurl.com:{port}");
0
On

the Url class have a Port property so you should be able to

new Url("https://www.myurl.com") { Port = 9000};