1

Im new to C# and am struggling to understand this: I'm trying to use one of the .NET 6.0 Uri constructors, namely this one: Uri(Uri baseUri, string? relativeUri);

My code looks like this:

string baseUrl = "https://example.com";
string? route = null;
Uri test = new Uri(baseUrl, route);

But I get this error:

'route' may be null here. 
Argument 2: cannot convert from string to bool
Muleskinner
  • 14,150
  • 19
  • 58
  • 79
  • 1
    There is no `Uri` constructor taking two string parameters, see https://learn.microsoft.com/en-us/dotnet/api/system.uri.-ctor. You propbably meant `new Uri(new Uri(baseUrl), route);` – Klaus Gütter May 19 '23 at 06:56
  • your second paramaeter should be bool and not string. For more clarity Check this: https://stackoverflow.com/questions/4835269/how-to-check-that-a-uri-string-is-valid – Sam May 19 '23 at 06:58

2 Answers2

3

Uri(Uri baseUri, string? relativeUri);

Okay, but you did not do that. You passed (string, string?).

If you want to call the (Uri, string?) constructor, do so:

Uri baseUrl = new Uri("https://example.com");
string? route = null;
Uri test = new Uri(baseUrl, route);

The error message you get is from the compiler picking the closest overload from what you have given it, which would be (string, bool). And since your second parameter is not a bool, you get this error.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

your second parameter should be bool and not string Check here for more: How to check that a uri string is valid

Mark Correct or Accepted if you get your answer.

Sam
  • 471
  • 2
  • 5
  • 9