C#为System.Uri属性指定默认值
我有一个关于RESTfulAPI的项目,我有一个基本URL作为属性,每次我都需要在上面添加部分(这在我的方法中)。我需要(a)声明默认(未更改)路径,然后(b)关于如何添加到URL的帮助。例如:C#为System.Uri属性指定默认值,c#,properties,C#,Properties,我有一个关于RESTfulAPI的项目,我有一个基本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
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..
};
}
以下代码表示(将此字符串转换为System.Uri
),但不能将字符串转换为System.Uri
:
(System.Uri) "http://192.100.106.657:8811/some/part/here/version1/api";
public System.Uri URL { get; set; } = new System.Uri("http://192.100.106.657:8811/some/part/here/version1/api");
您应该实例化System.Uri
:
(System.Uri) "http://192.100.106.657:8811/some/part/here/version1/api";
public System.Uri URL { get; set; } = new System.Uri("http://192.100.106.657:8811/some/part/here/version1/api");
这回答了你的问题吗?对于您得到的错误是重复的。这是否回答了您的问题?非常感谢,工作做得很好。关于如何在末尾添加部分的任何想法(参考问题的“public APIParamaters myParams=new apiparamates”部分)。非常感谢你!