Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Enrico 47 posts 252 karma points
    Jul 06, 2017 @ 15:20
    Enrico
    0

    Umbraco Rest API and UmbracoApiController CORS conflicting

    Hi everyone,

    I managed to install the new Umbraco Rest API and I was starting creating an Angularjs App with Restangular. I wanted create a method to export the website menu and use it in the Angular JS app. In the new Umbraco Rest Api there is no method to return the children of the root node without also returning all the properties. This has a big drawback for websites with lot of contents. In order to populate dynamically the menu you have also to load all the contents. Therefore, I thought to create a API which inherits from the UmbracoApiController and exports the menu. The problem is with the CORS policy. The Umbraco Rest Api CORS policy conflicts with the usual CORS policy I have always used to make Umbraco API controllers accessible from a different domain. You can find the post at https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/63841-Web-API-2-and-cross-origin-resource-sharing-CORS

    I started digging in the source code of the Umbraco Rest API, https://github.com/umbraco/UmbracoRestApi, and I found this attribute [DynamicCors] on https://github.com/umbraco/UmbracoRestApi/blob/master/src/Umbraco.RestApi/Controllers/UmbracoHalControllerBase.cs.

    I thought that adding the attribute on my API controller inheriting from UmbracoApiController could have solved the issue, but it didn't.

    Is there anything that I can do to solve my issue?

    Thanks in advance

    Enrico

  • Enrico 47 posts 252 karma points
    Jul 06, 2017 @ 16:00
    Enrico
    100

    I found the solution! The solution was setting the header from the controller method.

    I will share it for anyone who may need it.

    public class MenuController : UmbracoApiController
    {
    
        [HttpGet]
        public HttpResponseMessage GetJsonNavigation()
        {
            var linkItems = new List<LinkItem>();
    
            var homepage = Umbraco.ContentAtRoot().First();
            var menuItems = homepage.Children.Where("Visible");
    
           linkItems.Add(new LinkItem{Id = homepage.Id, Name = homepage.Name, ParentId = 0});
            foreach (var item in menuItems)
            {
    
                if (item.DocumentTypeAlias == "Shortcutlink")
                {
                    // todo
                }
                else
                {
                    linkItems.Add(new LinkItem { Id = item.Id, Name = item.Name, ParentId = item.ParentId });
                }
    
                var subMenuItems = item.Children.Where("Visible");
                foreach (var sub in subMenuItems)
                {
                    if (sub.DocumentTypeAlias == "Shortcutlink")
                    {
                        // todo
                    }
                    else
                    {
                        linkItems.Add(new LinkItem { Id = sub.Id, Name = sub.Name, ParentId = sub.ParentId });
                    }
                }
    
            }
    
            var response = Request.CreateResponse(HttpStatusCode.OK, linkItems);
            response.Headers.Add("Access-Control-Allow-Origin", "*");
    
            return response;
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft