Web Studio is here! An enhanced experience to make it easier to create, preview, and collaborate on your website contentLearn More
Over the years, we've made significant improvements to the way you can use Agility CMS within your .NET Applications, specifically around the way you Access Content, Page Management, and URL Redirections.
Let's take a deeper dive into each of these topics.
In .NET MVC and older legacy .NET applications, content was accessed using a Content Repository.
For example:
//filter products by a particular category
int categoryID = 20; //ContentID of the category
string categoryRowFilter = string.Format("ProductCategoryID = {0}", categoryID);
string sortByTitle = "Title ASC";
IList productsFilteredByCategory = module.Products.Items(categoryRowFilter, sortByTitle);
//get a subset of products from the list
int skip = 10; //number of products to skip
int take = 10; //number of products to take
string rowFilter = ""; //not filtering by anything
IList products = module.Products.Items(rowFilter, sortByTitle, take, skip);
//get products by their exact Content IDs
int contentID = 2343;
string contentIDs = "1232,43543,234";
//GetById and GetItemsByIDs is found in Agility.Web.Extentions
Product productByID = module.Products.GetById(contentID);
IList productsByIDs = module.Products.GetItemsByIDs(contentIDs);
//sort products by IDs
string sortIDs = "23,19,20,22,21";
IList productsSortedByIDs = module.Products.SortByIDs(sortIDs);
In MVC4 applications, Views and Partial Views were used to render HTML with Models, where as .NET Core applications used View Components.
In .NET7 and later, content is now accessed using our Fetch API. Many benefits come from moving towards this approach with our Fetch API, including a Global CDN with caching and performance benefits.
We've made configurations separate, but made to work together. Dependency Injection is used to inject the Fetch API service on start up, which wires up the HTTPS client for the ability to call our Fetch API:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
services.AddHttpContextAccessor();
services.AddHttpClient<FetchApiService>();
services.AddSingleton<AgilityRouteTransformer>();
}
You can inject the Fetch API service anywhere in your project (View Components, Controllers, Etc):
private readonly FetchApiService _fetchApiService;
private readonly AppSettings _appSettings;
public SiteHeader(FetchApiService fetchApiService, IOptions<AppSettings> appSettings)
{
_fetchApiService = fetchApiService;
_appSettings = appSettings.Value;
}
Dynamic Helpers are used to take JSON data and deserializes it to create strongly typed classes:
var getItemParameters = new GetItemParameters()
{
ContentId = Constants.SiteHeaderContentId,
Locale = locale
};
var content = await _fetchApiService.GetContentItem(getItemParameters);
var siteHeader = DynamicHelpers.DeserializeTo<ContentItemResponse<Agility.Models.SiteHeader>>(content);
Set your strongly typed model and pass it to the View(s) you are rendering:
var siteHeaderModel = new SiteHeaderModel()
{
SiteHeader = siteHeader.Fields,
SitemapPages = deserializedSitemap
};
You can now also generate Model Types for content definitions using the Agility CLI.
Our new approach is more flexible and supports all the features of our Fetch API.
In older .NET applications, Page Management was handled with a global.asax
file using routes.
In .NET7 and later, you now have the ability to opt-in/out of Page Management if you choose to.
Opting-in to Page Management is as simple as downloading the Agility Core Nuget Package and adding your endpoints.
In older .NET applications, URL Redirections are enabled by default. This cannot be changed (opt-in/out or create your own)
In .NET7 and later, you can create your own redirect middleware and don't have to use Agility's option for redirects.
Adding URL Redirections to your application is as simple as downloading the Agility Core Nuget Package and adding your redirects to the Startup.cs file.