IInternalUrlProvider
Create internal URLs for data items programmatically
You can create custom short internal URLs (for example, ~/news(...)) for your data types programmatically. To do so, you should:
- Create your format for internal links to data items by implementing the interface
IInternalUrlProvider
. - Register the provider.
Note. The code sample here uses a static page data folder "Demo.News".
Expand the sample code for Demo.News
Important: If you also define short URL names for data types (in the GUI or when creating a static data type), the internal URL format defined in the custom provider will be used when links are inserted. However, both sets of formats will equally work if used.
Creating a custom internal URL provider
To create a custom internal URL provider that will set the format for internal data links, you need to implement the interface IInternalUrlProvider
.
using System; using System.Linq; using Composite.Core.Extensions; using Composite.Core.Routing; using Composite.Core.Types; using Composite.Data; namespace Demo { class CustomDataUrlProvider : IInternalUrlProvider { private readonly string _shortTypeName; private readonly Type _type; private readonly Type _keyType; public CustomDataUrlProvider(string shortTypeName, Type type) { _type = type; _keyType = type.GetKeyProperties().Single().PropertyType; _shortTypeName = shortTypeName; } public string BuildInternalUrl(IDataReference reference) { string serializedKey = ValueTypeConverter.Convert<string>(reference.KeyValue); if (string.IsNullOrEmpty(serializedKey)) { return null; } return "~/{0}({1})".FormatWith(_shortTypeName, serializedKey); } } }
Download CustomDataUrlProvider.cs
Registering the internal link format with the provider
Now that you've created a custom internal URL provider, you need to register it. You can do it on the application startup:
using System.Linq; using Composite.Core.Application; using Composite.Core.Routing; using Composite.Data; using Composite.Data.DynamicTypes; namespace Demo { [ApplicationStartup] public class Startup { public static void OnBeforeInitialize() { } public static void OnInitialized() { DynamicTypeManager.EnsureCreateStore(typeof(Demo.News)); var newsDataType = DataFacade.GetAllInterfaces().FirstOrDefault(u => u.Name == "News"); InternalUrls.Register(newsDataType, new CustomDataUrlProvider("news", newsDataType)); } } }
onInitialized()
. Code will throw an exception when accessing methods related to data façade in OnBeforeInitialize()
.EnsureCreateStore
before making use of it.Converting internal links into public URLs
The internal links you get with this sample will look like ~/news(...)
, and you will be able to insert them in the content referring to a specific news item. However, you'll need to explicitly convert them so that they resolve as public URLs to data items when accessed on the website.
You can do it programmatically by creating a custom URL converter, or you can make use of RoutedData<T> to build a list-to-detail view on a page (see below the sample CMS Function that does that.)
Using the sample
To test this sample, first create the provider and add it to your website:
- In Visual Studio, create a class library project using the sample code above for the data type, provider and the application startup.
- Build and copy the resulting DLL to
~/Bin
on your website. - Restart the server.
Next, add the "News" data folder to a page:
- Log in to the CMS Console.
- From the "Content" perspective, create a page.
- Add the "News" data folder to the page.
- Add a few news items to the folder.
Finally, create and use a CMS Function to list the news items and view their details:
- From the "Functions" perspective, create a Razor function.
- Replace the code in the function with the code below.
- Save the function.
- Insert the function on the page from the steps above.
- Save and publish the page.
Now you can open the page and click the links to individual news items to view the news item's details.
Requirements:
C1 CMS version 5.0 or later