Extended routing for Episerver Content Delivery API
Episerver Content Delivery API delivers content through Urls like: http://<your-site-url>/api/episerver/v1.0/content/<contentReference>. During my hackday I thought I should extend the CMS routing so that it is possible to use the same "Friendly URLs" as in CMS. My idea was to hook into the CMS routing and after a content has been routed look at the Accept header and in case it was "application/json" rewrite the request to the Web API controller in EPiServer Content Delivery.
So I did not want to do any Json serialization but rely on Content Delivery API for that (same with access checks) and I did not want todo a redirect but stay on the friendly url.
It turned out to work quite straightforward. I found a small issue in CMS (which I reported an bug for) that made it a bit more complicated than it should have needed to be, but it was quite easy to workaround.
The result looks like this for an alloy site when applying Accept header:
One thing that often is a problem with REST is that you often gets huge Json responses even if you are only interested in some property. A nice solution to that problem is Graph QL, for this hackday though I did not have time to look into adding Graph QL support (perhaps a topic for next hackday). What I did instead was adding a partial router that routes to individual properties for content and then I added an action filter to the WebAPI where I filter the Json result to the routed property. So then it is possible to do requests for individual properties like below where Name and MainContentArea are properties on the content item with url http://localhost:63139/en/alloy-track:
The code is available at https://github.com/jbearfoot/ContentDeliveryExtendedRouting (there is also a nuget package there that you can install). The module does not need any configuration or setup of either CMS or ContentDelivery its just reference it and it should just work.
Disclaimer:
This is nothing offically supported by EPiServer, you are free to use it as you like at your own risk.
Comments