繁体   English   中英

C# 为 System.Uri 属性分配默认值

[英]C# Assigning default value to System.Uri property

我有一个关于 RESTful API 的项目,我将基本 URL 作为属性,我每次都需要在其上添加部分(在我的方法中)。 我需要 (a) 声明默认(未更改)路径,然后 (b) 关于如何添加到 URL 的一些帮助。 例子:

    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 !!!
}

这是抛出错误,我不知道如何纠正。 另外,我以后如何添加到 URL,例如,我正在尝试

    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..
    };
}

以下代码表示(将此stringSystem.Uri )但字符串不能转换为System.Uri

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

您应该实例化System.Uri

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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