I am working on refactoring some code and want to create WebApi services to do so. I have created an initialization pipeline that registers custom routes. I am still able to follow base sitecore ssc webapi methods if need be.
My issue is that I am not able to get my Post controller method to bind to my ajax post request JSON object. High level I have a controller method called Post, the method parameter is an object with two public properties, FirstName
and LastName
. In my ajax request I set the contentType
to application/json
and set the following as data:
{"FirstName":"first", "LastName": "last"}
The pieces are as follows:
Controller
public class MyApiController : ServicesApiController
Controller Method
[HttpPost] public IHttpActionResult Search([FromBody]SearchRequest searchRequest)
Model Class
[Serializable] public class SearchRequest { public string FirstName { get; set; } public string LastName { get; set; } }
CustomRoute
DefaultRouteMapper.MapRoutes(config); var routes = config.Routes; routes.MapHttpRoute("Test_Search", "sitecore/api/test/{controller}", new { controller = "MyApi" });
Ajax Method
jQuery.ajax({ type: 'Post', contentType: 'application/json', data: { "FirstName" : "first", "LastName" : "last" }, url: '/sitecore/api/test/myapi' });
I appreciate anyone's help.
/sitecore/api/myapi/{put_your_method_here}