[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:
There is anyway to do this? We only want to return a custom robots.txt from the backend depending in some configuration.
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.
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?
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
And we have the next controller
When I tried to access /robots.txt is throwing the following error:
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!
I think you want a standard
Controller
rather than anUmbracoApiController
. You only want anUmbracoApiController
if you want Umbraco to automatically register a route for you. In this case, the automatic route would beUmbraco/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 anUmbracoHelper
it should go away if you use a normalController
. It's only trying to create one because anUmbracoApiController
needs one.Thanks for your answer.
I'm going to need inject one dependency to obtain the values for the Robots.txt. Using the next code:
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!
is working on a reply...