Table of Contents

How To Organize your Code in Razor

In simple scenarios you have some Razor files containing a bit of HTML and some code. As your solution grows, you'll want to organize your work better to ensure that you can maintain it. 2sxc offers various ways to do this:

Discover More in the Razor Tutorials

We have an rich series of Razor tutorials. You should really check them out 👍.


Reuse a Partial View with @Html.Partial(...) v12+

Razor templates can include other razor files with more Razor code inside them, using Html.Partial("_Something.cshtml"). This is a standard asp.net function to render another Razor file where you need it. You usually use it to make small component Razor files which might just show a button or something, and then call that file.

You can find examples in the tutorials

Share a .cs File as Library v10+

Sometimes you want to share C# code which isn't meant for HTML-output. For example, a security check. You can do this using CreateInstance(...).

If you:

  1. need to share code with razor and Webapi
  2. don't need razor specific features like @helper

You can create a .cs class file and share this across razor files AND WebAPI files.

To use it, you need something like:

@{
  var helper = CreateInstance("_Helper.cs");
}
Tip

The helper file should ideally inherit from Custom.Hybrid.Code14 (or similar base classes). in which case it will have have the same full APIs incl. the App and Content object just like the main file.

👉🏼 See examples in the tutorials

Reuse a Template Delegate Function generating HTML v5+

Template Delegates are a very old Razor feature, but they are quite hard to use.

They are similar to @helpers but work in both Oqtane and DNN.

👉🏼 See tutorial example.

Older / Alternative Methods (DNN only)

Reuse Snippets with @helper in Razor

Razor has a @helper syntax which allows you to create fragments and re-use them.

👉🏼 Discover this in the tutorials.

This will not work in Oqtane, as the newer Razor engines don't support this.

Reuse a Partial View with @RenderPage()

@RenderPage(...) does the same thing as @Html.Partial(...) (see above) but it will only work on DNN, not Oqtane.

Share a .cshtml File as Library of Sub-Templates

When you have a lot of components it may be easier to create a library of @helper commands. This library is just a normal .cshtml file - usually in a folder called shared or something, and you can then call these snippets and helpers from all your template files.

To use it, you need something like:

@{
  var helper = GetCode("_Helper.cshtml");
}

...older Razor API:

@{
  var helper = CreateInstance("_Helper.cshtml");
}

👉🏼 See examples in the tutorials

Razor Code-Behind

If your Razor file is getting kind of large because of C# functions, best place it in a Razor Code-Behind.


History

  1. RenderPage has always been part of Razor so part of 2sxc v2
  2. CreateInstance for .cshtml was introduced ca. v6
  3. CreateInstance for .cs was introduced in 2sxc v10.01
  4. Code-Behind Introduced in 2sxc 11.0
  5. Code-Behind deprecated in 2sxc 12 because it's not compatible with Oqtane