c# - How to define response error metadata in swagger/swashbuckle WebAPI 2 -
i have figured out how create response object metadata in swashbuckle:
[route("x/{y:guid}")] [httpget] [swaggerresponse(httpstatuscode.ok, "bla", typeof(bladto))] public ihttpactionresult getsomething([fromuri] guid someguid) { bla returnobject; try { returnobject = _service.get(someguid); } catch (databaseexception databaseexception) { modelstate.addmodelerror("databaseexception", databaseexception.message); return badrequest(modelstate); } return ok(returnobject); }
i still looking examples define request object meta data , meta data 400 errors etc. pointers appreciated.
object metadata of operation detected swashbuckle automatically. object metadata of errors can specified adding more swaggerresponseattribute
s.
here's example:
[route("x}")] [httppost] [swaggerresponse(httpstatuscode.created, "bla", typeof(bladto))] [swaggerresponse(httpstatuscode.conflict, type = typeof(errorresponse))] [swaggerresponse(httpstatuscode.badrequest, type = typeof(errorresponse))] public ihttpactionresult createbla(bladto bla) { bladdto returnobject; try { returnobject = _service.create(bla); } catch (databaseexception databaseexception) { var error = new errorresponse { message = databaseexception.message); return content(httpstatuscode.badrequest, error); } catch (someotherexception ex) { var error = new errorresponse { message = ex.message); return content(httpstatuscode.conflict, error); } return ok(returnobject); } public class errorresponse { string message { get; set; } }
Comments
Post a Comment