简体   繁体   中英

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

I have a WCF Web API. Some endpoints accept DataContracts which obviously contain different properties and datatypes. 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. Is there a simple way to instead, throw back a HTTP 400 Bad Request response (and possibly the offending property).

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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