Model Validation in ASP.NET Web API By Mike Wasson|July 20, 2012 268 of 294 people found this helpful
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using System.Web.Http.ModelBinding; namespace MyApi.Filters
{
public class ValidateModelAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
actionContext.Response = actionContext.Request.CreateErrorResponse(
HttpStatusCode.BadRequest, actionContext.ModelState);
}
}
}
}
Corresponding result pasted below:
HTTP/1.1 400 Bad Request
Content-Type: application/json; charset=utf-8
Date: Tue, 16 Jul 2013 21:02:29 GMT
Content-Length: 331 {
"Message": "The request is invalid.",
"ModelState": {
"product": [
"Required property 'Name' not found in JSON. Path '', line 1, position 17."
],
"product.Name": [
"The Name field is required."
],
"product.Weight": [
"The field Weight must be between 0 and 999."
]
}
}
Pasted below exception the link, global exception handling in webapi
Model Validation in ASP.NET Web API By Mike Wasson|July 20, 2012 268 of 294 people found this helpful的更多相关文章
- Model Validation in ASP.NET Web API
Model Validation in ASP.NET Web API 原文:http://www.asp.net/web-api/overview/formats-and-model-binding ...
- 【ASP.NET Web API教程】3 Web API客户端
原文:[ASP.NET Web API教程]3 Web API客户端 Chapter 3: Web API Clients 第3章 Web API客户端 本文引自:http://www.asp.net ...
- Exception Handling in ASP.NET Web API webapi异常处理
原文:http://www.asp.net/web-api/overview/error-handling/exception-handling This article describes erro ...
- 【ASP.NET Web API教程】6.4 模型验证
本文是Web API系列教程的第6.4小节 6.4 Model Validation 6.4 模型验证 摘自:http://www.asp.net/web-api/overview/formats-a ...
- Exception Handling in ASP.NET Web API
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErr ...
- 【ASP.NET Web API教程】2.1 创建支持CRUD操作的Web API
原文 [ASP.NET Web API教程]2.1 创建支持CRUD操作的Web API 2.1 Creating a Web API that Supports CRUD Operations2.1 ...
- 【ASP.NET Web API教程】4.3 ASP.NET Web API中的异常处理
原文:[ASP.NET Web API教程]4.3 ASP.NET Web API中的异常处理 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...
- 【ASP.NET Web API教程】6 格式化与模型绑定
原文:[ASP.NET Web API教程]6 格式化与模型绑定 6 Formats and Model Binding 6 格式化与模型绑定 本文引自:http://www.asp.net/web- ...
- [转]Enabling CRUD Operations in ASP.NET Web API 1
本文转自:https://docs.microsoft.com/en-us/aspnet/web-api/overview/older-versions/creating-a-web-api-that ...
随机推荐
- 第八章 springboot + mybatis + 多数据源
http://www.cnblogs.com/java-zhao/p/5413845.html
- onDestroy 和 onBackPressed、onfinish 的区别
在android 开发中, 我容易不区分 onDestroy .onBackpress.onFinish. 其实,可以这样理解: (1)onDestory 的使用,是销毁了activity的实例在内存 ...
- 【JAVA】 UIMnager
Java'中的几种Look and Feel 1.Metal风格 (默认) String lookAndFeel = "javax.swing.plaf.metal.MetalLookAnd ...
- PHP中面向对象的关键字
php面向对象中常用的关键字有final.static.const (1)final: 1,final不能修饰成员属性 2,final只能修饰类和方法 作用: 使用final修饰的类不能被子类继承 使 ...
- thinkphp2
- SOLD原则
借鉴: 1. 单一职责原则 单一职责原则 (Single Responsibility Principle,SRP) 指出,每个方法或类应当有且仅有 一个改变的理由.这意味着每个方法或类应当做一件事情 ...
- C fopen
格式:文件指针名=fopen(文件名,使用文件方式) 参数:文件名 意义"C://TC//qwe.txt" 文件C:/TC/qwe.txt"qwe.txt" 和 ...
- protected $appends in Laravel
protected $appends = ['icon']; public function getIconAttribute(){ return Gravatar::src($this->em ...
- 【转】24Cxx 系列EEPROM通用程序及应用
关于I2C 学习的时候介绍得最多的就是24C02 这里存储EEPROM了,但学的时候基本只是讲讲简单的I2C 的总线数据传输而已,即使先gooogle上搜索也绝大部分这这样的文章,很少有说到如何在实际 ...
- JMeter学习-035-JMeter调试工具之二---Debug PostProcessor
前文 JMeter学习-034-JMeter调试工具之一---HTTP Mirror Server讲述了HTTP镜像服务器在调试请求入参时的实例应用.此文我们讲述另一种测试脚本调试工具的使用. 前置处 ...