Wednesday, 15 January 2014

c# - ASP.NET MVC 4 Routing For Specific Controller Action -



c# - ASP.NET MVC 4 Routing For Specific Controller Action -

i have next controller/action:

public class somethingcontroller { ienumerable<something> search(datetime mindate, datetime maxdate, bool summaryonly = true){} }

the thought summaryonly parameter doesn't have specified, mindate , maxdate must be.

can offer routes above?

you may seek next route:

public static class webapiconfig { public static void register(httpconfiguration config) { config.routes.maphttproute( name: "search", routetemplate: "api/search/{mindate}/{maxdate}/{summaryonly}", defaults: new { summaryonly = routeparameter.optional, controller = "something", action = "search" } ); config.routes.maphttproute( name: "defaultapi", routetemplate: "api/{controller}/{id}", defaults: new { id = routeparameter.optional } ); } }

and then:

public class somethingcontroller : apicontroller { [httpget] public ienumerable<something> search(datetime mindate, datetime maxdate, bool summaryonly = true) { ... } }

and request endpoint this:

/api/search/2013-02-08/2013-02-10/

or:

/api/search/2013-02-08/2013-02-10/false

c# asp.net-mvc-4 asp.net-web-api-routing

No comments:

Post a Comment