So you want to create Web API controllers in a Sitecore 8 solution? Use Sitecore.Services.Infrastructure.Web.Http.ServicesApiController
and add Sitecore.Services.Core.ServicesControllerAttribute
to namespace your controller. Read Developer’s Guide to
Sitecore.Services.Client (PDF) from page 61 and onward. Controllers will be reachable at:
/sitecore/api/ssc/{namespace}/{controller}/{id}/{action}
That’s it. No routing setup, no pipeline fiddling. Quick and easy! Just note that Sitecore does not support dependency injection in the controller – at least out of the box.
Below is a full example that shows async/await in a Web API controller – note the aspnet:UseTaskFriendlySynchronizationContext
appSetting in Web.config.
You might also want to check my post on disabling of Sitecore Web API filters.
This was so helpful. Thank you.
Andreas, I set this up per your specification, but the API call just returns a blank screen. When I check Fiddler, it’s returning a 404 error. I’ve tried both the attribute-based name and the “real” name based on the actual namespace/controller in use. I assume there’s nothing extra needed in the global.asax or the like? Is there other configuration needed that I might be missing? I’m basically putting your code on top of a blank Sitecore 8.1 Update 1 site. Thanks.
Hi Ken
It should work just out of the box – no configuration needed in Global.asax or similar. Have you tried my exact sample code? The Sitecore routing is defined in a way so that you must include
{id}
parameter in the url. Also a namespace like[Sitecore.Services.Core.ServicesController("example1.test.andreas")]
will result in urls like/sitecore/api/ssc/example1-test/andreas/{id}/{action}
.If it doesn’t help you, then send me an email with your code.