簡體   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