Skip to content

If you use AppCode/Links.cs Outside of the Context of LinkRazor your get an Error #38

Description

@jeremy-farrance

This code looks normal. In particular, on line 12, @link.WindowAuto
https://github.com/2sic/2sxc-content-app/blob/master/bs5/Link/List%20of%20links.cshtml

We want and expect this same functionality to work in our own custom Views. For example (see line 19, 'target='):

@* filename: Content\ISER\ContentPlus_Text-with-button-list.Part-Link.cshtml *@
@inherits AppCode.Razor.LinksRazor
@using AppCode.Data

@* This Partial Template is used in our custom TextImagePlus which has 0, 1, or many Links as a related Entity named Topic *@

@{
  var item = MyModel.Get<TextImagePlus>("item");
}

<div class="col-12">
  <div class="row">
    @foreach (Link link in AsList<Link>(item.Topic))
    {
      <div class="btn-topic pl-3">
        @if (link.IsNotEmpty("Link"))
        {
          <a href='@link.Link' target='@link.WindowAuto' title="@link.Title" class="btn btn-primary">
            @link.Get("LinkText")
          </a>
        }
      </div>
    }
  </div>
</div>

This throws an error because in AppCode/Data/Link.cs, line 66, throws an exception when the CurrentPageUrl is not set ahead of time.

The only place I can find an example of this getting set ahead of time is in AppCode/Razor/LinksRazor.cs. Line 31.

But that is not our context. Instead we have a Content Type we call TextImagePlus (its the TextImage CT with a couple of extra fields). We've added a field named Topics which is just 0/1/many Link entities.

So when try to loop through those entities with @foreach (Link link in AsList<Link>(item.Topic)), the link.WindowAuto gets called without LinkRazor's CurrentPageUrl getting set ahead of time... error.

Here are some attempts at working around the issue by modifying Link.cs:

    private bool LinkIsInternal()
    {
      if (_linkIsInternal.HasValue) return _linkIsInternal.Value;
      // var currentUrl = CurrentPageUrl ?? throw new ArgumentException("CurrentPageUrl is not set"); // original
      // var currentUrl = CurrentPageUrl ?? "https://dev.iser-society.org/"; // hack it for now << works!!
      // Globals.DependencyProvider.GetService<INavigationManager>().NavigateURL(); // can't do this in DNN 9.13.x (.DependencyProvider is internal)
      // var currentUrl = CurrentPageUrl ??= Kit.Link.To(); // can't use here and no idea (yet) how to get the ILinkService from here
      var currentUrl = CurrentPageUrl ??= Globals.NavigateURL(); // obsolete, being removed in DNN v11

      _linkIsInternal = Link.Contains(currentUrl) // Link to the same page
            || Link.StartsWith("/") // absolute link in same site, eg. "/about-us"
            || Link.StartsWith("#") // hash-link on same page eg "#about-us"
            || Link.StartsWith("."); // relative link from this page eg "../about-us"
      return _linkIsInternal.Value;
    }

I like the idea of replacing the throw exception with a call to Link.To(), but I don't know "how to get there, from here."

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions