Hello World - ASP.Net Core

Here's how to create a hello world page with Lynicon.

First create this class in HelloContent.cs in the Models folder (create one if there isn't one):

public class HelloContent : PageContent
{
	public MinHtml Message { get; set; }

	public HelloContent()
	{
		InitialiseProperties(this);
	}
}

This model class holds the data for the content which will be shown on the page.  It inherits from PageContent to provide it with standard HTML page metadata fields and to indicate it is being used to provide the content for a whole web page. The MinHtml class is used to store HTML markup, and the editor for this class allows minimal markup. Content classes when constructed should not have null values for reference types because in some contexts the editor to use is determine from the type of the reference stored in a property rather than the declared type of the property.

Now create a controller to use this model:

public IActionResult Index(HelloContent data)
{
	return View(data);
}

Very straightforward! The name of the parameter has to be 'data' by convention. Lynicon will fetch the appropriate content item and provide it to the controller through this variable.

Now create a basic view, it should be in the Views/Hello folder entitled Index.cshtml:

@model Lynicon_Test.Models.HelloContent
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @Model.Message
    </div>
</body>
</html>

Notice the Message property since it is typed as MinHtml does not require Html.Raw to be used

The last piece is to add a route in Startup.cs:

app.UseMvc(routes =>
{
	routes.MapLyniconRoutes();
	routes.AddDataRoute<HelloContent>("hello", "hello/{_0}", new { controller = "Hello", action = "Index" });
	//routes.MapRoute(
    //	name: "default",
    //	template: "{controller=Home}/{action=Index}/{id?}");
});

The line being added here is the call to AddDataRoute<ContentType>(). This adds a special route which is where the CMS magic happens. You supply it with the content type and a url template which by convention uses '{_0}', '{_1}', '{_2}' etc to indicate parts of the url that are used to index into the set of content items of the specified type.

Notice the catch-all conventional route is commented out.  Generally you will want to do this as otherwise content pages where there is no content may get caught by this route and throw errors or look like there is actually content there.  But there is no problem in using a combination of conventional routes and content routes.

Now you can create you first content item with Lynicon.  To do this, start up the site, log in with the administrator account from before if you are not already logged in, and navigate to /Lynicon/Items.

Click the '+' button on the bar entitled 'Hello' to see the url template for adding new pages.  Enter 'world' into the url element box and click the 'new page' icon.

This takes you to this page:

Enter the text 'Hello World' as shown into the Message box (a restricted HTML editor).  You will notice the SAVE button goes red.  Click it to save your changes and update the current page view to the left.  You will now see the page shows 'Hello World'.  If you click Log Out and go to /Hello/World you will see it as a public site user does.