Razor Tutorial - Home (17.10.0)
Below you'll find a list of topics this tutorial covers. The easiest way to work with it is to look at an example, then look at the code shown below the examples.
Basic
Quick Reference for Razor APIs
Quick Summary of most APIs for reference. This is still WIP...
Razor Basics
Learn how to use variables and loops, encode HTML, use 🚀😁💪 Emojis. Also advanced stuff like re-using code or setting page titles and SEO stuff.
Basics: Reuse Code and Templates Across Files
You can place common templates, hlp and functions into shared files - both .cshtml
and .cs
. And you can split Razor files into code and templates.You can place common templates, hlp and functions into shared files - both .cshtml
and .cs
. And you can split Razor files into code and templates.
Basics: Link between Pages and Views
Learn how to use Link.To(...) to link to specific pages or to link to the same page with certain URL parameters.
Basics: Set Page Title, Keywords etc. and Headers
Use the IPageService to set page properties, base tags and headers - even JsonLD and OpenGraph.
Basics: Get Information about Site, Page, User, etc.
To enable Hybrid Razor which works on Dnn and Oqtane, we need standards so that our Dynamic Code can access information about the page, module etc.
- Work with the Context (Page, Site, etc.)
Whenever you need to know about the current environment, you need Context information. These are the contexts you should know about: Platform Site Page Module View User
Content and Images
Show Content and Data Entities on a Page
In most cases a template will run in a context - so something prepared data for the template, which should now be visualized. These examples assume you're working with 2sxc, which lets editors work with content - and your template only needs to visualize it.
Images, Automatic Resizing and Responsive Pictures
Learn the basics of Images, Image Resizing and Pictures.
Multi-Language Content and Resources (i18n) 🌐
2sxc is super-powerful with multiple languages. It's actually one of the best multi-language solutions in all known CMS solutions out there.
Replace Content Placeholders using TemplateService (new 🌟)
Data and DataSources
Data
Access App-Data directly or from an App-Query. Learn to query the data in C# using LINQ and work with SQL and JSON
Data: List-Details Samples
Show data from DNN, the current App, DataSources or SQL, CSV etc. Also includes list/details (parent/child) examples.
Data: Work with Data from Queries - Entities, SQL, CSV and more
Show data from DNN, the current App, DataSources or SQL, CSV etc. Also includes list/details (parent/child) examples.
- Example from CSV
See an example how to use a Query to get Data from a CSV file as data source.
- Various SQL Samples
Use SQL in Queries or in Code to get more data.
- Use DNN APIs to get DNN Data
DNN also provides a lot of data about users, pages etc. In this example, we'll list all the pages (tabs) in DNN and mark the one we're on. Please note that the DNN API isn't very consistent - so sometimes things use ID , sometimes Id , so best…
Data: LINQ (Language Integrated Query)
The following examples show how to use LINQ (Language INtegrated Query) to sort, filter and group data. This is for quick work in your views - for more extensive querying, we recommend the Visual Query Designer.
Data: JSON
Easily work with JSON data.
DataSources: Use in C# Code
DataSources will usually be used in a VisualQuery, but you can also use them in C# for advanced scenarios. Learn about the Kit.Data API
DataSources: Create Custom Dynamic DataSources
Create custom DataSources directly in the App DataSources folder.
Advanced Settings and Automation
- Settings in 2sxc
Settings allow your code to use predefined settings across all apps and sites. And they allow you to overide a setting at any level of the application. You can read more about the settings in the docs 📕 . - Use PageService.Activate Features like JS/CSS
2sxc 12.04 helps with many advanced challenges like these: Make sure that certain scripts are only loaded once (like jQuery or FancyBox) Make sure that all apps use the same version of these scripts Starting things in the right order
RazorBlade and Koi
RazorBlade Tutorials
These examples help you quickly do hard stuff, once you've mastered the basics above
RazorBlade Fluent Html5 API
RazorBlade 3 contains over 130 objects with hundreds of properties so you can quickly generate Html5 tags correctly and safely.
Koi Tutorials
These examples help you do cool stuff, once you've mastered the basics above
- Use Koi to Adjust to the Page CSS Framework
Learn how to use Connect.Koi to ensure your module/template has the right CSS frameworks, to either auto-include otherwise, show messages to the admin or generate different output. This uses Connect.Koi . Some notes before we start Koi works like…
JavaScript, turnOn, WebAPIs / REST
JavaScript
2sxc offers powerful helpers to just get things done - or to do things your way.
turnOn
Use the turnOn feature to execute Javascript functions from Razor files
- turnOn Tutorial
Learn how to execute JavaScript functions using the 2sxc turnOn. Extend basic usage by passing data, awaiting conditions and more.
WebApi / JSON Endpoints Examples
These examples show how to create and use WebApi. This isn't Razor, but also uses C#. You'll often need this when creating JavaScript and SPA apps.
Edit UI and Formulas
Customize Edit UI / UX - Toolbars
Sometimes you want to customize what the editor will experience - using special toolbars or custom input fields. Note that this only applies to 2sxc.
Customize Edit UI - Fields
Create custom input fields and customize WYSIWYG to your needs.
- 2sxc Custom Input Fields (11.02+)
2sxc 11 makes it very easy to create custom input fields using standard WebComponents . This example shows the most basic case - just a dummy message (so not a real input field). You can use it to learn about things like: Naming conventions for…
Customize Edit-UI - Formulas (new!)
Add custom UI logic to your edit forms with formulas.
Advanced / Other
Hybrid - Advanced Scenarios for Razor on Dnn AND Oqtane
Most Razor is identical for DNN and Oqtane, but in some cases you wish to access platform specific objects or use certain features which require different code in Dnn and Oqtane. Here's how to switch between such code.
Crazy 2sxc Advanced Examples
These are really advanced examples and meant more to give guidance for specific questions. Don't worry if you don't understand these
- Inspect a Content-Block
In this example, we'll see what content-blocks are in the system, and where they are in use.
Future Tutorials (Work-In-Progress)
- Linking around in dnn
- Navigation links
- Permission examples
- Work with icon-fonts, font-awesome etc.
- Using data from other apps
- Customizing Search Results
- Customizing search from data