C# ASP.NET WebApi route template not working with uri parameters -
i have asp.net webapi application following controller , route:
webapiconfig.cs
var constraintresolver = new defaultinlineconstraintresolver(); constraintresolver.constraintmap.add("validdate", typeof(dateconstraint));
controller.cs
[httpget] [route("deleted/{from:validdate?}/{to:validdate?}", name = "getdeleteddata")] [swaggerresponse(httpstatuscode.ok, type = typeof(ienumerable<simpleclass>), description = "response")] public async task<httpresponsemessage> get( [fromuri]string = "", [fromuri]string = "" ) { }
constraint
public class dateconstraint : ihttprouteconstraint { public bool match( httprequestmessage request, ihttproute route, string parametername, idictionary<string, object> values, httproutedirection routedirection ) { object value; if (!values.trygetvalue(parametername, out value)) return false; var attribute = new dateattribute(); return attribute.isvalid(value); } }
the above route hit though pass following url ie not passing , parameters controller hit , nothing happen.
http://localhost:65190/products/deleted?adfear=2016-07-01 03:30:05&adfaewr=2016-07-01 03:30:05
how make sure route hit when correct parameters passed or else throw 404 not found error?
why re-invent wheel.
attribute routing in asp.net web api 2:route constraints
there exists datetime
constraint.
also can make last date optional if provided date, filter using date current date.
[httpget] [route("deleted/{from:datetime}/{to:datetime?}", name = "getdeleteddata")] [swaggerresponse(httpstatuscode.ok, type = typeof(ienumerable<simpleclass>), description = "response")] public async task<httpresponsemessage> get(datetime from, datetime? = null) { //.... }
based on comments can this
//get products/deleted [httpget] [route("deleted")] public async task<httpresponsemessage> get() { //... } //get products/deleted/2016-01-01 //get products/deleted/2016-01-01/2016-03-31 [httpget] [route("deleted/{from:datetime}/{to:datetime?}", name = "getdeleteddata")] public async task<httpresponsemessage> get(datetime from, datetime? = null) { //... }
this should handle 3 cases
//get products/deleted //get products/deleted/{from} //get products/deleted/{from}/{to}
update
if both parameters mode optional
//get products/deleted - including query string hit //get products/deleted?adfear=2016-07-01 03:30:05&adfaewr=2016-07-01 03:30:05 //get products/deleted/2016-01-01 //get products/deleted/2016-01-01/2016-03-31 [httpget] [route("deleted/{from:datetime?}/{to:datetime?}", name = "getdeleteddata")] public async task<httpresponsemessage> get(datetime? = null, datetime? = null) { //... }
update2
based on conversation
you can create filter model
public class daterangefilter { public datetime? { get; set; } public datetime? { get; set; } }
and can use in action.
// products/deleted // products/deleted?from=2016-01-01&to=2016-03-31 [httpget] [route("deleted", name = "getdeleteddata")] public async task<httpresponsemessage> get([fromuri]daterangefilter filter) { //... }
also remember model validation either via filter or within action.
Comments
Post a Comment