A first page
A page needs three more pieces: a content type, its registration, and something to render it.
// Models/IArticle.cs: a content type is an interface; Relatude.DB writes the class
using Relatude.CMS.Models;
using Relatude.DB.Nodes;
namespace MySite.Models;
public interface IArticle : IPage {
[HtmlProperty] string Body { get; set; }
}// Program.cs
using MySite.Models;
using Relatude.DB.Datamodels;
var builder = WebApplication.CreateBuilder(args);
builder.AddRelatudeDB(o => o.OnDatamodelInit = (dm, _) => {
dm.AddCmsContentModel(); // IItem, IPage, the templates and what they build on
dm.Add<IArticle>(); // the site's own types
});
builder.AddRelatudeCMS()
.AddMvcRendering()
.AddRendering(o => o.RenderRazorView<IArticle>()); // articles render with the view "Article"
var app = builder.Build();
app.UseRelatudeCMS();
app.MapRelatudeCMS();
app.Run();@* Views/Shared/Article.cshtml *@
@using MySite.Models
@model IArticle
<h1>@Model.Name</h1>
@Html.Raw(Model.Body)An article needs an address to have a URL. This creates one on the first start:
builder.AddRelatudeDB(o => o.OnStoreOpenBackground = db => {
if (db.Query<IArticle>().Count() > 0) return;
var article = db.CreateAndInsert<IArticle>(a => { a.Name = "Hello world"; a.Body = "<p>It works.</p>"; });
db.UpdateAddress(article.Id, "hello-world");
});GET /hello-world now renders Views/Shared/Article.cshtml with the article as its model. Calling
AddRelatudeDB twice is fine: every call configures the same options.