I'm building a website that is a SPA and I want to use Umbraco as a back end.
The sites content is currently coming from static JSON files, I want Umbraco to replace these JSON files by genrating the JSON based on what is in the CMS.
As a first step I have put the website with its static files in to an Umbraco Installation. Created a home node with a template.
This is working correctly. I have also aranged all the content for the JSON file in to logical nodes in the Umbraco content tree.
Take these URLs for an example
/data/nl/news.json
/data/en/news.json
How can I map BOTH of these URLs to a specific content node in Umbraco?
In my case I have:
-Home (Template with SPA)
-News (THIS IS THE NODE I WANT THE URLS TO MAP TO)
-NewsItem
-NewsItem
When the request hits that node I want to read the URL and then go and get the correct content from the children and return it as JSON.
Not 100% sure if I should be trying to set up some custom routes so the URL's hit a controller, or the IContentFinder, or possibly a combination of both.
Then you don't need a custom URL provider and content finder. We set the default controller and have an action method that looks like this (some parts were simplified since they aren't relevant to your question):
{
// Variables.
var page = MappingUtility.MapTypicalPage(CurrentPage, Umbraco, UmbracoContext);
var isSpa = Request.QueryString["spa"] == "true";
// Normal page or single-page app page?
if (isSpa)
{
// For some unknown reason, Umbraco was returning a 404 when this action method
// returned JSON. This fixes that by forcing a 200 response.
Response.StatusCode = 200;
// Return JSON.
var data = PageDataUtility.GetPageData(page, Request);
return Json(data, JsonRequestBehavior.AllowGet);
}
else
{
return CurrentTemplate(page);
}
}
Custom Routing and IContentFinder
I'm building a website that is a SPA and I want to use Umbraco as a back end.
The sites content is currently coming from static JSON files, I want Umbraco to replace these JSON files by genrating the JSON based on what is in the CMS.
As a first step I have put the website with its static files in to an Umbraco Installation. Created a home node with a template.
This is working correctly. I have also aranged all the content for the JSON file in to logical nodes in the Umbraco content tree.
Take these URLs for an example
How can I map BOTH of these URLs to a specific content node in Umbraco?
In my case I have:
When the request hits that node I want to read the URL and then go and get the correct content from the children and return it as JSON.
Not 100% sure if I should be trying to set up some custom routes so the URL's hit a controller, or the IContentFinder, or possibly a combination of both.
Any ideas?
No replies :-( is my question clear enough?
Might be easier to do with a query string. That's what we do for Jacuzzi: https://www.jacuzzi.com/en-us/hot-tubs?spa=true
Then you don't need a custom URL provider and content finder. We set the default controller and have an action method that looks like this (some parts were simplified since they aren't relevant to your question):
You can read about how to set a default controller here: https://our.umbraco.org/documentation/reference/routing/custom-controllers#change-the-default-controller
I assume you've already managed some way of creating URL's that contain the language, so I won't go into how we did that for Jacuzzi.
I solved this problem in the end by using URL rewriting.
Incase someone else has this problem:
If you don't have it installed already install the IIS rewrite module. https://www.microsoft.com/en-us/download/details.aspx?id=47337
Add a rewrite rule to your webconfig (I guess you can do this at IIS level too) so inside my WebConfig file the rule looks like this.
Job done, hope this helps someone else.
is working on a reply...