Asp.net-Web-Api
有沒有類似的東西Bind(和xclude=‘P執行______’)乙一世nd(和XCl在d和=‘磷r這p和r噸是’)Bind(Exclude=‘Property’)對於asp.net web api?
我試圖在 web api 控制器中從我的 Post Action 中排除一個屬性,是否有類似
[Bind(Exclude="Property")]asp.net web api 的東西?這是我的模型:
public class ItemModel { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } }我想在 Post Action 中排除 Id,因為它是自動生成的,但我需要在 Get Action 中返回它。
我知道我可以有兩個模型,一個用於我的 Post 操作,一個用於我的 Get 操作,但我試圖只用一個模型來做到這一點。
我更喜歡映射模型,但這可以通過檢查請求是否是 ShouldSerialize 方法中的 POST 來實現:
public class MyModel { public string MyProperty1 { get; set; } public string MyProperty2 { get; set; } public bool ShouldSerializeMyProperty2() { var request = HttpContext.Current.Request; if (request.RequestType == "POST") return false; return true; } }您的方法名稱是以 ShouldSerialize 為前綴的屬性名稱。
請注意,這適用於 JSON。對於 XML,您需要將以下行添加到您的配置中:
config.Formatters.XmlFormatter.UseXmlSerializer = true;