我有點困惑如何在我的WebAPI控制器中使用DTO。我正在使用實體框架的數據庫第一個概念。生成下面的實體數據模型:如何在WebAPI的控制器中使用DTO?
//Generated class by EDM:
public partial class Address
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Address()
{
this.Member = new HashSet<Member>();
}
public int Id { get; set; }
public string Street { get; set; }
public Nullable<short> Street_Number { get; set; }
public Nullable<decimal> Zip { get; set; }
public string City { get; set; }
public string Country { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Member> Member { get; set; }
}
要使用數據註釋我需要一個AddressDTO
因爲每次定義,當我需要修改數據模型將再次產生的電火花和數據註釋都將丟失。
所以接下來,我定義了AddressDTO
:
public class AddressDTO
{
public int Id { get; set; }
[Required]
[StringLength(100,ErrorMessage="The max. length of the street name is 100 characters.")]
public string Street { get; set; }
public Nullable<short> Street_Number { get; set; }
[Required]
public Nullable<decimal> Zip { get; set; }
[Required]
[RegularExpression(@"[a-z,A-Z]",ErrorMessage="Only characters are allowed.")]
public string City { get; set; }
[Required]
public string Country { get; set; }
}
而且控制器看起來像下面的代碼:
[RoutePrefix("api/address")]
public class AddressController : ApiController
{
private PersonEntities db = new PersonEntities();
// GET: api/Address
[HttpGet]
[ResponseType(typeof(AddressDTO))]
public async Task<IHttpActionResult> GetAll()
{
var addressList = await db.Address.ToListAsync();
return Ok(addressList);
}
}
當我開始在RESTAPI瀏覽器中顯示的結果,我總是得到以下json結果:
[
{
"Member": [ ],
"Id": 1,
"Street": "Example Street",
"Street_Number": 1,
"Zip": 12345.0,
"City": "New York",
"Country": "USA"
},...
]
但是我需要以下desir編輯結果:
[
{
"Street": "Example Street",
"Street_Number": 1,
"Zip": 12345.0,
"City": "New York",
"Country": "USA"
},...
]
有沒有人有想法我怎麼能解決這個問題?
創建僅包含要暴露給API的屬性的包裝類 – Fabio
僅因爲要使用某些數據註釋屬性就不需要創建其他模型。你可以簡單地使用[MetadataTypeAttribute](https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.metadatatypeattribute(v = vs.110).aspx) –
@RezaAghaei你可以舉個例子碼? – yuro