TackleMcClean 🏅
11/26/2023, 7:31 PMAnders Bjerner
11/27/2023, 3:12 PMByRouteContentApiController
class is responsible for looking up a content node from it's path/route.
https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Cms.Api.Delivery/Controllers/ByRouteContentApiController.cs#L110-L113
If a requested path isn't found, Umbraco asks it's IRequestRedirectService
if a redirect exists for this route, and if so, returns a 301 redirect to that URL. The default implementation will only look for Umbraco's internal redirects.
https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Cms.Api.Delivery/Services/RequestRedirectService.cs
Theoretically you might be able to replace the default implementation with your own, and then look for Skybrud Redirects as well. But the GetRedirectRoute
method must return an instance of IApiContentRoute
, so this is tied to content. Eg. you can't support redirects to media or external URLs.
The corresponding controller for media doesn't support redirects - probably because Umbraco doesn't have internal redirects for media - only for content.
https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Cms.Api.Delivery/Controllers/ByPathMediaApiController.cs
Ideally the ByRouteContentApiController
class should have an event/notification that developers and packages can hook into. This would let a package like Skybrud Redirects check whether a redirect exists for the specified path, and then handle it accordingly. But this requires changes to Umbraco.
Umbraco also handles content and media individually, so if you'd use Skybrud Redirects for creating /my-fancy-pdf
and redirect users to the actual PDF URL, should this be handled by content API or the media API? You might not know what's behind the path ahead of asking the API(s) 😮TackleMcClean 🏅
11/27/2023, 3:17 PMAnders Bjerner
11/27/2023, 3:20 PMIRedirectsService
should have the logic that you need.Anders Bjerner
11/27/2023, 3:28 PMcsharp
using Skybrud.Umbraco.Redirects.Models;
using Skybrud.Umbraco.Redirects.Services;
using Umbraco.Cms.Web.Common.Controllers;
namespace Umbraco13 {
public class RedirectsController : UmbracoApiController {
private readonly IRedirectsService _redirectsService;
public RedirectsController(IRedirectsService redirectsService) {
_redirectsService = redirectsService;
}
public object? GetByPath(string path) {
IRedirect? redirect = _redirectsService.GetRedirectByUrl(Guid.Empty, path);
return redirect ?? (object) NotFound();
}
}
}
So a call to /umbraco/api/Redirects/GetByPath?path=/hi
would trigger the following response:
json
{
"id": 2,
"key": "10987395-e3cf-4a18-8ee5-058a08a5faaf",
"rootKey": "00000000-0000-0000-0000-000000000000",
"path": "/hi",
"queryString": "",
"url": "/hi",
"destination": {
"id": 1082,
"key": "614b6874-e078-4879-a9be-4275164df9d1",
"name": "",
"url": "/hej-verden/",
"query": "",
"fragment": "",
"fullUrl": "/hej-verden/",
"type": "content",
"culture": ""
},
"createDate": "2023-11-27T15:25:35.220Z",
"updateDate": "2023-11-27T15:25:35.220Z",
"type": "temporary",
"permanent": false,
"forward": false
}
It's the redirect model returned as JSON. So at least for this quick controller, the frontend would then have to piece the destination URL back together on it's own.TackleMcClean 🏅
11/27/2023, 4:29 PMTackleMcClean 🏅
11/27/2023, 5:34 PM(>= 10.0.0 && < 12.999.0)
) but I'm not hacker enough to even try 😄Anders Bjerner
11/27/2023, 5:43 PMAnders Bjerner
11/27/2023, 8:46 PMTackleMcClean 🏅
11/27/2023, 8:54 PM