简体   繁体   中英

C# Assigning default value to System.Uri property

I have a project which is about RESTful API and I have as a property the basic URL, on which I need to add parts each time (that's in my methods). I need (a) to declare the default (unchanged) path, and then (b) some help on how do I add to the URL. Example:

    public partial class APIParamaters
{
    public System.Uri URL { get; set; } = (System.Uri) "http://192.100.106.657:8811/some/part/here/version1/api";   //throws error !!!
}

This is throwing an error and I don't know how to correct. Also, how do I later add to the URL, for example, I am trying

    class MyTest
{
    public string SpecialPart = "Excellent";

    public APIParamaters myParams = new APIParamaters
    {
        URL = URL + SpecialPart + "FirstCall",     //trying to do: "http://192.100.106.657:8811/some/part/here/version1/api/Excellent/FirstCall"
        SomethingElse = "Ok"
        //etc..
    };
}

The following code means (cast this string as System.Uri ) but string can not be cast as System.Uri :

(System.Uri) "http://192.100.106.657:8811/some/part/here/version1/api";

You should instantiate System.Uri :

public System.Uri URL { get; set; } = new System.Uri("http://192.100.106.657:8811/some/part/here/version1/api");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM