Copied to clipboard

Flag this post as spam?

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


  • Ammar Atassi 2 posts 72 karma points
    Jul 28, 2020 @ 14:22
    Ammar Atassi
    0

    Request Umbraco API on a server

    I have a backoffice deployed on server and have the source code on my local computer.

    I want to create an Umbraco API and be able to request it but I don't know where do I have to deploy the API on my server ? In which folder ? Is it the generated project dll to put in the bin folder ?

  • David Armitage 505 posts 2073 karma points
    Aug 01, 2020 @ 03:26
    David Armitage
    0

    Hi Ammar,

    You can either pop the code in the an App_Code folder. Most developers usually have a separate project to store all the custom .net code. Either is fine.

    Personally for a small project I would probably just pop it in the App_Code folder and be up and running very quickly.

    Here is an example of one of my API methods. You should be able to pretty much copy what's there.

    using Umbraco.Web.WebApi;
    using System.Web.Http;
    using Umbraco.Core.Logging;
    using Website.Core.Models.EntityModels;
    using Website.Core.Helpers;
    using System;
    namespace Website.Core.Controllers
    {
        public class SearchApiController : UmbracoApiController
        {
            private readonly IBlogHelper _blogHelper;
            private readonly ILogger _logger;
    
            public SearchApiController(IBlogHelper blogHelper)
            {
                _blogHelper = blogHelper;
            }
    
            //umbraco/Api/SearchApi/SearchBlog
            [HttpGet]
            public BlogSearch SearchBlog(
                                string serviceGuid = null,
                                string sectorGuid = null,
                                string regionGuid = null,
                                string officeGuid = null,
                                string staffGuid = null,
                                string keywords = null,
                                string tags = null,
                                string parentGuid = null,
                                int currentPage = 1,
                                int itemsPerPage = 1000)
            {
    
                BlogSearch blogSearch = new BlogSearch();
    
                try
                {
                    blogSearch.CurrentPage = currentPage;
                    blogSearch.ItemsPerPage = itemsPerPage;
                    blogSearch.ServiceGuids = serviceGuid != null ? serviceGuid : "";
                    blogSearch.SectorGuids = sectorGuid != null ? sectorGuid : "";
                    blogSearch.RegionGuids = regionGuid != null ? regionGuid : "";
                    blogSearch.OfficeGuids = officeGuid != null ? officeGuid : "";
                    blogSearch.StaffGuids = staffGuid != null ? staffGuid : "";
                    blogSearch.Keywords = keywords != null ? keywords : "";
                    blogSearch.Tags = tags != null ? tags : "";
                    blogSearch.ParentKey = parentGuid != null ? parentGuid : "";
                    return _blogHelper.Search(blogSearch);
                }
                catch (Exception e)
                {
                    _logger.Error<SearchApiController>("SearchBlog | Exception: {0} | Message: {1}", e.InnerException != null ? e.InnerException.ToString() : "", e.Message != null ? e.Message.ToString() : "");
                }
    
                return blogSearch;
            }
        }
    }
    

    And this is the url to call the method.

    /umbraco/Api/SearchApi/SearchBlog
    

    Regards

    David

Please Sign in or register to post replies

Write your reply to:

Draft