c# - Route attributes range constraint vs range attribute validation -
with new attribute routing , constraints seems border why should use attributes validate dto`s gets blurry more , more.
i can range validation on 2 ways - consider first approach has action complex class + weight property -
when should use approach , advantages vs other approach?
[range(0,500)] public int weight {get;set;}
vs
[get("{id:range(0, 500)}")] public machine getmachine(int weight) { }
the first approach result in bad request.
the second approach result in not found request.
your last 2 sentences summarize well. in case of [range]
, sets validation of input can tell caller request bad. in second, it's defining rules matching url route, used sending requests different routes, not validating input. second useful, example, if have ids of different formats want handle different methods.
in short, ux first "i must sending invalid data" , second "i must have wrong url."
update:
here's demonstration of meant how use route attribute different formats:
[get("/users/{id:int}")] public user getuserbyid(int id) { } [get("/users/{email:regex(^[^@]+@[^@]$)}")] public user getuserbyemail(string email) { } [get("/users/{username}")] public user getuserbyname(string username) { }
Comments
Post a Comment