Search code examples
dotnetnuke2sxc

How do I get portal name in 2sxc v.16.5.0 cshtml file that inherits Custom.Hybrid.Razor14?


I have a cshtml file that inherits Custom.Hybrid.Razor14. I can't change this inheritage since I need access to Kit property to update meta tags of the page. And I need to access portal name. Dnn objects are available only within files/classes that inhering some Dnn namespace class. I've tried to create a separate file helper in .cs file. But I didn't figure out how to access portal object from there to get its name. Documentation of 2sxc has huge amount of info but it seems useless for my case since I didn't succeed to find a missing peace of info there. So how do I access portal name from there?

Here's my latest attempt to make it work. It's a .cs file with a helper that I was going to use in a .cshtml file that inherits Custom.Hybrid.Razor14.

using System;
using System.Globalization;
using ToSic.Razor.Blade;
using ToSic.Sxc.Data;

public class DnnHelper : Custom.Hybrid.CodeTyped//Custom.Dnn.Razor12
{
    public string PortalName { get; set; }

    public DnnHelper()
    {
        PortalName = PortalSettings.PortalName; // throws exception as it can't find PortalSettings from here
    }
}

Solution

  • Here is a more specific answer that should just work with sample output.

    @inherits Custom.Hybrid.Razor14
    
    @using ToSic.Sxc.Services
    @using ToSic.Sxc.Dnn.Run
    
    @{
      var MyDnn = GetService<IDnnContext>();
    }
    
    <pre>  
    MyDnn.Portal
      @MyDnn.Portal.GetType()
      .PortalId:          @MyDnn.Portal.PortalId
      .PortalName:        @MyDnn.Portal.PortalName
      .EMail              @MyDnn.Portal.Email
    
    MyDnn.Tab
      @MyDnn.Tab.GetType()
      .TabID              @MyDnn.Tab.TabID
      .TabName            @MyDnn.Tab.TabName
    
    MyDnn.Module
      @MyDnn.Module.GetType()
      .ModuleID:          @MyDnn.Module.ModuleID
      .ModuleTitle:       @MyDnn.Module.ModuleTitle
    
    MyDnn.User
      @MyDnn.User.GetType()
      .UserID             @MyDnn.User.UserID
      .Username           @MyDnn.User.Username
      .DisplayName        @(MyDnn.User.DisplayName ?? "Anon E. Mouse")
      .IsInRole("Hoser")  @MyDnn.User.IsInRole("Hoser")
      .Roles.Join()       @string.Join(", ", MyDnn.User.Roles)
    </pre>