Copied to clipboard

Flag this post as spam?

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


  • Hugo Becerra 12 posts 82 karma points
    Sep 11, 2019 @ 11:34
    Hugo Becerra
    0

    Umbraco v8 - Custom routing for UmbracoApiController

    Hi everyone,

    We are having some problem adding a custom routing setting an UmbracoApiController to answer a request.

    We have the next custom route

    routes.MapRoute("RobotsTextFile", "robots.txt", new { controller = "RobotsText", action = "Index" });
    

    And we have the next controller

    [Route("api/[controller]")]
    public class RobotsTextController : UmbracoApiController
    {
        [System.Web.Http.HttpGet]
        public ActionResult Index()
        {
            var result = new ContentResult();
            result.Content = "test";
            return result;
        }
    }
    

    When I tried to access /robots.txt is throwing the following error:

    enter image description here

    There is anyway to do this? We only want to return a custom robots.txt from the backend depending in some configuration.

    Thanks in advance for your help!

  • Steve Megson 151 posts 1024 karma points MVP c-trib
    Sep 11, 2019 @ 12:58
    Steve Megson
    1

    I think you want a standard Controller rather than an UmbracoApiController. You only want an UmbracoApiController if you want Umbraco to automatically register a route for you. In this case, the automatic route would be Umbraco/Api/RobotsText/Index.

    The error you're getting seems to be Umbraco failing to create an UmbracoHelper for the request. I'm not sure quite why that's failing, but since your controller doesn't actually need an UmbracoHelper it should go away if you use a normal Controller. It's only trying to create one because an UmbracoApiController needs one.

  • Hugo Becerra 12 posts 82 karma points
    Sep 11, 2019 @ 13:14
    Hugo Becerra
    0

    Thanks for your answer.

    I'm going to need inject one dependency to obtain the values for the Robots.txt. Using the next code:

        public class RobotsTextController : Controller
    {
    
        private readonly ISettingsService _settingsService;
    
        public RobotsTextController(ISettingsService settingsService)
        {
            _settingsService = settingsService;
        }
    
        [System.Web.Http.HttpGet]
        public ActionResult Index()
        {
            var result = new ContentResult();
            var accessItems = _settingsService.ConfigurationPage.AccessoItemsAsKeyValuePairs;
            result.Content = accessItems.ToString();
            return result;
        }
    }
    
    
    public class RegisterHomeControllerComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
    
            composition.Register<RobotsTextController>(Lifetime.Request);
    
    
        }
    }
    

    Throw the same error. I don't know why is falling when try to create an UmbracoHelper but I think is the problem to solve in this case, don't you think?

    Thanks for your help!

Please Sign in or register to post replies

Write your reply to:

Draft