简体   繁体   English

在不正确的数据类型 WCF Web API 上返回错误请求 (HTTP 400) 错误

[英]Returning Bad Request (HTTP 400) errors on incorrect data types WCF Web API

I have a WCF Web API.我有一个 WCF Web API。 Some endpoints accept DataContracts which obviously contain different properties and datatypes.一些端点接受明显包含不同属性和数据类型的 DataContracts。 If a request contains a property with an incorrect data type (ie A string in an integer property) the value comes through as null after being serialized.如果请求包含具有不正确数据类型的属性(即整数属性中的字符串),则该值在被序列化后为 null。 Is there a simple way to instead, throw back a HTTP 400 Bad Request response (and possibly the offending property).有没有一种简单的方法来代替,返回 HTTP 400 错误请求响应(可能还有违规属性)。

Thanks谢谢

There are several ways :有几种方法:

if(param == null){
    throw new HttpException(400, "Bad Request, param is invalid");
    //or 
    return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad Request, param is invalid");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM