c# - ASP.NET Web API action routing doesn't not work -
i'm new web api, know 1 of difference between web api , mvc web api uses http method choose method invoke convention.
now added action parameter route, think there's went wrong knowledge, route doesn't work.
here's controller, main problem need map signin , signup method. (other restful methods work fine.)
public class userscontroller : apicontroller { private model1container db = new model1container(); public ienumerable<user> getusers() { return db.userset; } [httppost] public ihttpactionresult signin(string account, string password) { ... } [httppost] public ihttpactionresult signup(user user) { ... } public ihttpactionresult getuser(int id) { ... } public ihttpactionresult putuser(int id, user user) { ... } public ihttpactionresult deleteuser(int id) { ... } }
what i've tried add direct constraint on top 2 routes both methods,
config.routes.maphttproute( name: "userssignin", routetemplate: "api/users/signin", defaults: new { controller = "users", action = "signin" } ); config.routes.maphttproute( name: "userssignup", routetemplate: "api/users/signup", defaults: new { controller = "users", action = "signup" } ); config.routes.maphttproute( name: "restfuluser", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional }, constraints: new { controller = "users" } );
but turns out [post] "api/users/signup" , "api/users" map signup method, "api/users/signin" not succeed.
and here's how try api in postman
please point out did go wrong ..
if using attribute routing need differentiate between 2 routes. once using routes or nothing on controller itself. can't mix them convention-based routes.
review attribute routing in asp.net web api 2
[routeprefix("api/users")] public class userscontroller : apicontroller { private model1container db = new model1container(); [httpget] [route("")] //matches api/users public ienumerable<user> getusers() { return db.userset; } [httppost] [route("signin")] //matches post api/users/signin public ihttpactionresult signin(string account, string password) { //... } [httppost] [route("signup")] //matches post api/users/signup public ihttpactionresult signup([frombody]user user) { //... } [httpget] [route("{id:int}")] //matches api/users/5 public ihttpactionresult getuser(int id) { //... } [httpput] [route("{id:int}")] //matches put api/users/5 public ihttpactionresult putuser(int id,[frombody]user user) { //... } [httpdelete] [route("{id:int}")] //matches delete api/users/5 public ihttpactionresult deleteuser(int id) { //... } }
Comments
Post a Comment