asp.net - WebApi route not working -
here route:
config.routes.maphttproute( name: "createuser", routetemplate: "api/user/createuser", defaults: new { controller = "user", action = "createuser" } );
here endpoint:
[httppost] public async task<user> createuser([frombody] user user) { db.directusers.add(user); await db.savechangesasync(); return user; }
i sending post data:
{ "userid" : 1 "name" : "nick" "twitterhandel" : "x" }
to : http://localhost:56792/api/user/createuser
but im getting 404
ideally, not need add routes each action method. 1 route configuration enough normal api.
if need advanced routing, can @ web api 2's attribute routing.
public static void register(httpconfiguration config) { config.maphttpattributeroutes(); // alone enough. config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new {id = routeparameter.optional}); ... }
beside, should not access via http://.../api/user/createuser
. defeats purpose of using web api in action methods mapped http verbs.
so need httpost url creating new user.
Comments
Post a Comment