C# ServiceStack响应默认值
C# ServiceStack响应默认值,c#,
servicestack,C#,
servicestack,[默认值]数据注释与ORMLite一起使用。但是,它不适用于响应的默认值。是否有类似于响应DTO的[Default]属性的内容 考虑到以下准则: [Route("api/hello")] public class Hello { public string Ping { get; set; } } public class HelloResponse { public ResponseStatus ResponseStatus { get; set; } [Default
[默认值]
数据注释与ORMLite一起使用。但是,它不适用于响应的默认值。是否有类似于响应DTO的[Default]
属性的内容
考虑到以下准则:
[Route("api/hello")]
public class Hello {
public string Ping { get; set; }
}
public class HelloResponse {
public ResponseStatus ResponseStatus { get; set; }
[Default(typeof(string), "(nothing comes back!)")]
public string Pong { get; set; }
}
我希望Response DTO Pong属性有一个默认值(没有返回!),而不是空值。只需在构造函数中设置它。ServiceStack中的DTO是普通的C#对象。没什么特别的
public class HelloResponse
{
public HelloResponse()
{
this.Pong = "(nothing comes back!)";
}
public ResponseStatus ResponseStatus { get; set; }
public string Pong { get; set; }
}
类的构造函数将始终在对象初始值设定项中设置的任何属性之前运行:
var resp = new HelloResponse();
Console.WriteLine(resp.Pong); // "(nothing comes back!)"
resp = new HelloResponse
{
Pong = "Foobar";
};
Console.WriteLine(resp.Pong); // "Foobar"
只需在构造函数中设置它。ServiceStack中的DTO是普通的C#对象。没什么特别的
public class HelloResponse
{
public HelloResponse()
{
this.Pong = "(nothing comes back!)";
}
public ResponseStatus ResponseStatus { get; set; }
public string Pong { get; set; }
}
类的构造函数将始终在对象初始值设定项中设置的任何属性之前运行:
var resp = new HelloResponse();
Console.WriteLine(resp.Pong); // "(nothing comes back!)"
resp = new HelloResponse
{
Pong = "Foobar";
};
Console.WriteLine(resp.Pong); // "Foobar"