c# - web api: action parameter is null -
i have simple web api when test postman, method parameter null. i've looked , on other questions similar title haven't found answer.
here controller action:
[httppost] public member create([frombody] string payload) { var s = request.content.readasstringasync(); if (payload == null) { throw new argumentnullexception(nameof(payload)); } console.writeline(payload); console.writeline(s); return null; }
and here postman configuration:
in headers tab i've added content-type
application/json
.
when i'm debugging this, payload
variable allways null
, string s
contains
id = 98, status = rantocompletion, method = "{null}", result = ""
so doing wrong?
you should wrap string in model (object):
class model { public string payload {get;set;} } [httppost] public async task<member> create([frombody] model model) // wrap member in task , add async keyword { var s = await request.content.readasstringasync(); // add await here if (model.payload == null) { throw new argumentnullexception(nameof(model.payload)); } console.writeline(model.payload); console.writeline(s); return null; }
if don't want use model, try send plain string, "some payload"
, not wrap in json
.
Comments
Post a Comment