4

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:

  1. Controller

    public class MyApiController : ServicesApiController
    
  2. Controller Method

    [HttpPost]
    public IHttpActionResult Search([FromBody]SearchRequest searchRequest)
    
  3. Model Class

    [Serializable]    
    public class SearchRequest
    {
       public string FirstName { get; set; }
       public string LastName { get; set; }
    }
    
  4. CustomRoute

    DefaultRouteMapper.MapRoutes(config);
    
    var routes = config.Routes;
    
    routes.MapHttpRoute("Test_Search",
        "sitecore/api/test/{controller}",
         new { controller = "MyApi" });
    
  5. Ajax Method

    jQuery.ajax({
       type: 'Post',
       contentType: 'application/json',
       data: { "FirstName" : "first", "LastName" : "last" },
       url: '/sitecore/api/test/myapi'
    });
    

I appreciate anyone's help.

5
  • 1
    I can see one error in your code: you use for json : firstName and in complex type you have FirstName; Can you check ? Aug 18, 2017 at 4:50
  • What version of jquery are you using? Have you confirmed that the ajax request is definitely going out as a post? It looks like there is a comma missing after the object being passed to data.
    – Teeknow
    Aug 18, 2017 at 15:40
  • @Teeknow, sorry for the typo. In the Javascript there is a comma after data. Using JQuery 1.11.0. Yes I am sure the request in made as a post. I can see in Chrome development tools, the request is made with a post.
    – Derek Port
    Aug 18, 2017 at 15:47
  • @SitecoreClimber, after changing the properties in the JSON to match the case in my Model class, I still get the 404.
    – Derek Port
    Aug 18, 2017 at 15:50
  • @DerekPort no problem. Can you try removing your route and making the request to the default contorller route which should be the following: /sitecore/api/myapi/{put_your_method_here}
    – Teeknow
    Aug 18, 2017 at 15:52

3 Answers 3

1

It looks like you need to use the JSON.stringify() to pass an object.

jQuery.ajax({
   type: 'Post',
   contentType: 'application/json',
   data: JSON.stringify({ "firstName" : "first", "lastName" : "last" })
   url: '/sitecore/api/test/myapi'
});
1
  • No change after changing data parameter to use JSON.stringify.
    – Derek Port
    Aug 18, 2017 at 14:55
0

I have seen this issue before if u set default controller or method Sitecore has thrown 404 for me too.

Try setting the custom route as below:

RouteTable.Routes.MapRoute("CustomRoute1", "sitecore/api/test/{controller}/{action}");

More info on creating a custom route here - creating custom routes in sitecore

2
  • No luck using the approach above.
    – Derek Port
    Aug 21, 2017 at 15:55
  • Are you overriding any sitecore 404 code? Aug 21, 2017 at 17:49
0

I found my issue. There was another controller with the same name. Once I changed the name of the controller to something unique, I got the expected results.

I'm a bit surprised that I didn't get a different exception, but nonetheless I'm moving forward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.