Downloaded 328 times



































![ "opt-out" approach
"opt-in" approach
public class Product
{
public string Name { get; set; }
public decimal Price { get; set; }
[JsonIgnore]
public int ProductCode { get; set; }
}
[DataContract] public class Product
{
[DataMember] public string Name { get; set; }
[DataMember] public decimal Price { get; set; }
// omitted by default
public int ProductCode { get; set; }
}](https://image.slidesharecdn.com/asp-netwebapi-130529230752-phpapp02/75/The-Full-Power-of-ASP-NET-Web-API-36-2048.jpg)
![public object Get()
{
return new {
Name = "Alice",
Age = 23,
Pets = new List<string> { "Fido", "Polly", "Spot" } };
}
public void Post(JObject person)
{
string name = person["Name"].ToString();
int age = person["Age"].ToObject<int>();
}](https://image.slidesharecdn.com/asp-netwebapi-130529230752-phpapp02/75/The-Full-Power-of-ASP-NET-Web-API-37-2048.jpg)






![public class ProductMD
{
[StringLength(50), Required]
public string Name { get; set; }
[Range(0, 9999)]
public int Weight { get; set; }
}](https://image.slidesharecdn.com/asp-netwebapi-130529230752-phpapp02/75/The-Full-Power-of-ASP-NET-Web-API-44-2048.jpg)
![ [Required]
[Exclude]
[DataType]
[Range]
[MetadataTypeAttribute( typeof( Employee.EmployeeMetadata ) )]
public partial class Employee
internal sealed class EmployeeMetadata
[StringLength(60)]
[RoundtripOriginal]
public string AddressLine { get; set; }
}
}
[StringLength(60)]
[RegularExpression]
[AllowHtml]
[Compare]](https://image.slidesharecdn.com/asp-netwebapi-130529230752-phpapp02/75/The-Full-Power-of-ASP-NET-Web-API-45-2048.jpg)

![public class ModelValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
// Return the validation errors in the response body.
var errors = new Dictionary<string, IEnumerable<string>>();
foreach ( var keyValue in actionContext.ModelState )
{
errors[keyValue.Key] =
keyValue.Value.Errors.Select(e => e.ErrorMessage);
}
actionContext.Response =
actionContext
.Request
.CreateResponse(HttpStatusCode.BadRequest, errors);
}
}
}](https://image.slidesharecdn.com/asp-netwebapi-130529230752-phpapp02/75/The-Full-Power-of-ASP-NET-Web-API-47-2048.jpg)
![public class ModelValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
// Return the validation errors in the response body.
var errors = new Dictionary<string, IEnumerable<string>>();
foreach ( var keyValue in actionContext.ModelState )
{
errors[keyValue.Key] =
keyValue.Value.Errors.Select(e => e.ErrorMessage);
}
actionContext.Response =
actionContext
.Request
.CreateResponse(HttpStatusCode.BadRequest, errors);
}
}
}
protected void Application_Start() {
// ...
GlobalConfiguration
.Configuration
.Filters.Add(new ModelValidationFilterAttribute());
}](https://image.slidesharecdn.com/asp-netwebapi-130529230752-phpapp02/75/The-Full-Power-of-ASP-NET-Web-API-48-2048.jpg)










The document discusses various aspects of building RESTful web APIs with ASP.NET Web API, including content negotiation, model validation, serialization, and self-hosting. It provides code examples for adding validation filters, configuring formatters for different content types, and setting up a self-hosted API server. The document also references resources for learning more about RESTful architecture and building hypermedia-driven APIs with ASP.NET Web API.