Copied to clipboard

Flag this post as spam?

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


  • Paul Stoker 39 posts 72 karma points c-trib
    Feb 03, 2015 @ 18:24
    Paul Stoker
    0

    Production issue: How to access API route when ~/umbraco is filtered by IP Address & Domain Restrictions

    Something that has bugged me for a while is that I can't create an Umbraco API Controller that is consumed by say a page in the website since our production environment blocks requests to ~/umbraco.

    If I create a controller that's accessible by the following it's always blocked because it starts with ~/umbraco

    ~/umbraco/api/canyou/dosomething

    I have tried to create a custom route in the routing table but this doesn't seem to work.

    Can someone tell me if this is possible and how?

  • Richard Terris 273 posts 715 karma points
    Feb 04, 2015 @ 13:14
    Richard Terris
    100

    Paul,

    My suggestion would be using WebApiController instead of UmbracoApiController

    That way your URL would be ~/Api/Controller/Action

    Would that work for you?

    Richard

  • Paul Stoker 39 posts 72 karma points c-trib
    Feb 04, 2015 @ 16:05
    Paul Stoker
    0

    Hi Richard,

    I didn't think I could use the Umbraco backend this way. Turns out I can :-)

    public class SomeApiController : ApiController
    {
        private readonly UmbracoHelper umbracoHelper;
    
        public SomeApiController()
        {
            umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
        }
    
        public IEnumerable<string> GetAll()
        {
            return umbracoHelper.TypedContentAtRoot().Select(p => p.Name);
        }
    }
    

    And a route in the routing table:

    RouteTable.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
    

    Thanks

  • Richard Terris 273 posts 715 karma points
    Feb 04, 2015 @ 16:19
    Richard Terris
    0

    Yeah, the UmbracoApiController inherits from ApiController in .NET

    Unless you're using some of the extras in that controller like UmbracoHelper for example, then you can use the standard .NET controller.

    Even if you needed UmbracoHelper you could just instantiate it in your controller anyway.

    Glad that helped.

Please Sign in or register to post replies

Write your reply to:

Draft