My suggestion is to remove the "DefaultApi" route in RegsiterRoutes. (I assume you do not have a use for it) And then remove the {searchParameter} value in your route template. If you do this, you will not have to modify your javascript and it will route
correctly.
Andyrandy
Member
1 Points
14 Posts
How to write map/route for multiple Post
Mar 26, 2012 05:01 PM|LINK
Hi I have an API Controller which required multiple POST :
one for Save/Update :
public Order PostOrder(Order order) { }And, another for Search Order
public IQueryable<Order> SearchWithParameter(SearchOrderParameter searchParameter) { }I have added below Route method in Global.asax
routes.MapHttpRoute( name: "SearchWithParameter", routeTemplate: "api/{controller}/{action}/{searchParameter}", defaults: new { action = "post" } );In Js I am calling it as :
var SearchOrderParameter = new Object(); SearchOrderParameter.OrderID = 1197; $.ajax({ url: "http://localhost:9003/api/orders/SearchWithParameter", data: SearchOrderParameter, type: "Post", dataType: "json", success: function (value) { alert(value); }, error: function (jqXHR, textStatus, errorThrown) { alert(textStatus); } });But everytime this method calls PostOrder. Please guide me, What I am missing, so that it will call SearchWithParameter.
Thanks
Andy
davebettin
Member
313 Points
94 Posts
Re: How to write map/route for multiple Post
Mar 26, 2012 10:32 PM|LINK
Try removing the {searchParameter} in your template. Is this necessary, I don't see it in your example call.
@dbettin
Andyrandy
Member
1 Points
14 Posts
Re: How to write map/route for multiple Post
Mar 26, 2012 10:57 PM|LINK
"Try removing the {searchParameter} in your template. Is this necessary"
I tried after remove {searchParameter}...but again it call PostOrder
"I don't see it in your example call."
public IQueryable<Order> SearchWithParameter(SearchOrderParameter searchParameter) { }above is the place where I am calling searchParameter.
davebettin
Member
313 Points
94 Posts
Re: How to write map/route for multiple Post
Mar 26, 2012 11:01 PM|LINK
Can you post your entire OrdersController?
@dbettin
Andyrandy
Member
1 Points
14 Posts
Re: How to write map/route for multiple Post
Mar 27, 2012 12:53 AM|LINK
As in my first post those are only two methods in OrdersController.
davebettin
Member
313 Points
94 Posts
Re: How to write map/route for multiple Post
Mar 27, 2012 01:27 AM|LINK
There must be another route that is being selected. What other routes do you have in your global.asax?
@dbettin
Andyrandy
Member
1 Points
14 Posts
Re: How to write map/route for multiple Post
Mar 27, 2012 01:31 AM|LINK
Below is the code for RouteRegister
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapHttpRoute( name: "SearchWithParameter", routeTemplate: "api/{controller}/{action}/{searchParameter}", defaults: new { action = "post" } ); }davebettin
Member
313 Points
94 Posts
Re: How to write map/route for multiple Post
Mar 27, 2012 01:46 AM|LINK
To select the correct route, use the following Post request: /api/order/SearchWithParameter/somevalue.
Currently, it is selecting this route for your request /api/orders/SearchWithParameter/:
routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );@dbettin
Andyrandy
Member
1 Points
14 Posts
Re: How to write map/route for multiple Post
Mar 27, 2012 02:09 AM|LINK
Hi Dave, appreciate your help...can you please help me to write this in js.
davebettin
Member
313 Points
94 Posts
Re: How to write map/route for multiple Post
Mar 27, 2012 02:15 AM|LINK
My suggestion is to remove the "DefaultApi" route in RegsiterRoutes. (I assume you do not have a use for it) And then remove the {searchParameter} value in your route template. If you do this, you will not have to modify your javascript and it will route correctly.
@dbettin