c# - Post Method is not working in ASP.NET Core -
i trying create asp.net core web api. have post/get method per below:
[httpget] [route("api/getproffessionbyid/{id}")] public ihttpactionresult getproffessionbyid(int64 id) { allproffession model = new allproffession(); try { var result = model.getallproffession.where(i => i.id == id).firstordefault(); return ok(result); } catch (exception ex) { return internalservererror("something went wrong : " + ex.tostring()); } } [httppost] [route("api/saveperson")] private ihttpactionresult saveperson(personmodel model) { try { if (model.name != string.empty || model.weight != 0 || model.height != 0 || model.proffession != 0) { guid guid = guid.newguid(); random random = new random(); int = random.next(); model.id = i; return ok(model); } else { return badrequest(); } } catch (exception ex) { return internalservererror("something went wrong : " + ex.tostring()); } } my method working fine url postman. 
i've tried [frombody] well, not working
model:
public class personmodel { public int id { get; set; } public string name { get; set; } public int weight { get; set; } public int height { get; set; } public int proffession { get; set; } } webapiconfig.cs
public static void register(httpconfiguration config) { // web api configuration , services // web api routes config.maphttpattributeroutes(); config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { controllers = "api", id = routeparameter.optional } ); config.formatters.jsonformatter.supportedmediatypes.add(new mediatypeheadervalue("text/html")); }
maybe should change method public instead of private.
[httppost] [route("api/saveperson")] public ihttpactionresult saveperson(personmodel model) { //... }
Comments
Post a Comment