Table of Contents

Visual Studio Code - Guide

This guide will help you get VS Code Setup as best as possible for 2sxc development.

Tip

You'll often write code in your 2sxc Apps - either as C#/Razor or JavaScript. 2sxc is a very open system, so you can use any editor you like. For quick fixes and simple things, use the built-in editor, which is based on Monaco (VS Code Online). But for more sophisticated stuff we highly recommend VS Code.

Important

In 2024-05 the C# DevKit suddenly stopped working. We found a fix - pls update your app.sln to the newest sample below.

Prepare VS Code for 2sxc

VSCode is amazing right out of the box, but to really be productive, you need to do a few things:

  1. Install VS Code
  2. Install the C# DevKit extensions
  3. Configure each App with
    1. Intellisense (see below)
    2. Ignore the obj and .vs folders (see below)
  4. Check possible edge cases (see below)

With these preparations, VS-Code is able to assist in basic C# code. It can't provide IntelliSense for 2sxc specific APIs yet, so for that, read on.

Warning

The C# DevKit extension is still quite new and evolving. At the moment, various newer versions have issues which cause problems. Because of this, ATM recommend manually downgrading the C# extension to version 2.63.32.

Downgrade C# Extension

Configure an App for Razor IntelliSense

Apps are usually opened as a folder in VS-Code. The problem for IntelliSense is that it doesn't know which DLLs it should use. So you need to tell it. This is done by adding a .sln solution file and a .csproj project file.

Tip

Adding these files helps VSCode provide IntelliSense. But be aware that it can't help with dynamic code. To get the full benefit, use typed code.

Add the following two files to the root of your app:

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.002.0
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "App", "app.csproj", "{9F7A078F-99D5-4EF4-8EC0-C6B920FE679C}"
EndProject

#
# Visual Studio .sln File for 2sxc App
# This is necessary so that VS Code can perform intellisense in Razor
# It also requires a csproj file to exist as well
# 
# Read more and get help for issues on https://go.2sxc.org/vscode
#

# Addition 2024-05
# The following section is suddenly required by the C# DevKit
# See https://github.com/microsoft/vscode-dotnettools/issues/1151
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{9F7A078F-99D5-4EF4-8EC0-C6B920FE679C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{9F7A078F-99D5-4EF4-8EC0-C6B920FE679C}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{9F7A078F-99D5-4EF4-8EC0-C6B920FE679C}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{9F7A078F-99D5-4EF4-8EC0-C6B920FE679C}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
  GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal

More About the .csproj File

The following are some additional notes about the .csproj file, how it works and it's values.

At the top of the .csproj file, we detect if we're running in Dnn or Oqtane like this:

  <!-- First: Detect if it's running in Dnn, Oqtane-Production or Oqtane-Dev -->
  <PropertyGroup>
    <RunningInDnn Condition="Exists('..\..\..\..\bin\DotNetNuke.dll')">true</RunningInDnn>
    <RunningInOqtane Condition="Exists('..\..\..\Oqtane.Server.dll') Or Exists('..\..\..\bin\Debug\net8.0\Oqtane.Server.dll')">true</RunningInOqtane>
    <OqtaneIsProd Condition="Exists('..\..\..\Oqtane.Server.dll')">true</OqtaneIsProd>
    <OqtaneIsDev Condition="Exists('..\..\..\bin\Debug\net8.0\Oqtane.Server.dll')">true</OqtaneIsDev>
  </PropertyGroup>

This sets variables such as RunningInDnn and RunningInOqtane which you can use later in the file. You can see it in action in things such as:

  <!-- Settings for Dnn -->
  <PropertyGroup Condition="'$(RunningInDnn)' == 'true'">
    <!-- this is only applied, if the condition above is true -->
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>

GitIgnore Temporary Folders

Add these lines to your .gitignore file to prevent temporary files from being added to your repository:

.vs/
obj/
bin/

Check for Edge Cases - DNN with .net 4.7.2 / 4.8

If you're using DNN with .net 4.7.2 or 4.8, you may have to do some extra work. We're still not 100% sure what this is, since our dev PCs are always setup with all kinds of build tools where it works. According to research by Accuraty you may need to follow the instructions as noted on the C# extension.

The C# extension is auto-installed by the Dev-Kit, but there is this (quoted):

Note

Note: If working on a solution that requires versions prior to .NET 6 or non-solution based projects, install a .NET Framework runtime and MSBuild tooling.

  • Set omnisharp.useModernNet to false and set dotnet.server.useOmnisharp to true
  • Uninstall or disable C# Dev Kit we're not sure if this is correct any more!
  • Windows: .NET Framework along with MSBuild Tools
  • MacOS/Linux: Mono with MSBuild

According to our current understanding you don't need to do this is you have Visual Studio 2022 installed. We assume that already includes all the bits which VS Code needs as well.

We haven't been able to verify or simplify this, but if you're having trouble, do read the blog post by Accuraty as well.

Warnings when Using IntelliSense

Warning

IntelliSense can show you internal APIs which will change over time.

IntelliSense is an amazing productivity boost, but you should avoid using internal APIs.

To make this unlikely, we spent a LOT of time to clearly mark internal stuff. Avoid the following:

  1. Almost everything in the ToSic.Eav.* namespace is usually internal, so avoid using it
    1. Exception: ToSic.Eav.DataSource and ToSic.Eav.DataSources are really public
  2. Anything in a *.Internal namespace
  3. Anything in a *.Integration namespace
  4. Anything in a *.Backend namespace
  5. Anything in a *.Sys namespace
  6. Anything marked with [Obsolete]
  7. Anything marked with [EditorBrowsable(EditorBrowsableState.Never)] - IntelliSense will not show these APIs
  8. Properties beginning with an underscore, eg _Something

Known Issues when C# IntelliSense is not Working

Tip

Here we try to collect known issues and solutions.

  1. A razor file has the same name as a C# class in the AppCode Folder
    This will confuse the IntelliSense, since the Razor file will magically be seen as a class with the same name. Solution: Rename the Razor file to something else.

Configure an App for JavaScript IntelliSense

TODO: this is not yet documented


History

  • Added v16.07 2023-10
  • 2024-06-19 - Added more details about the .csproj file incl. date-version and correct DNN System.Net.Http reference

Shortlink: https://go.2sxc.org/vscode