c# - Web api with mvc 6 get element based on string -
i creating web api using mvc 6. trying element db. key in table string (email address). not have access database cant change key of table.
now when creating demo webapi able create controller extract items based on key int. when trying element string program crashes.
[route("api/[controller]")] public class todocontroller : controller { [httpget("{id:string}", name = "getbyidroute")] public iactionresult getbyid (string id) { var item = _items.firstordefault(x => x.id == id); if (item == null) { return httpnotfound(); } return new objectresult(item); } }
when trying access path (example.com/api/todo/key) key being string exception in startup.cs
the exception in browser reads:
system.invalidoperationexception constraint entry 'id' - 'string' on route 'api/todo/{id:string}' not resolved constraint resolver of type 'defaultinlineconstraintresolver'.
the part of startup.cs code breaks is:
// add mvc request pipeline. app.usemvc(routes => { routes.maproute( name: "default", template: "{controller}/{action}/{id?}", defaults: new { controller = "home", action = "index" }); });
i cant seem figure out why ain't allowed item key string. possible , if doing wrong?
just remove :string
. you're not constraining value of id
anyway - it's string in url.
this old blog post lists available constraints - , can see there's no :string
constraint, because don't need there be.
the constraints used give "more specific" constraints priority - e.g. "if part of url string representation of datetime
, use route" - string (in url), there's nothing constraint of :string
make more specific than, if see mean.
Comments
Post a Comment