Dvirski
Dvirski

Reputation: 321

Global helpers inside razor views in App_Code are not recognized

EDIT: After opening a new ASP.NET Web Application in Rider it works, so I'm now trying to figure out what is the key difference between the two projects.

I'm using Mono 6.8, specifically MVC version 5.3.2 with .NET version 4.7.2, with Jetbrains Rider IDE.E and Mac OS.

I performed a task to split some of our razor (.cshtml) views into two for different platforms, so I wanted to create global helpers to avoid code reuse.

For each view I created another view in /App_Code folder to hold shared helpers, but when building, these are not recognized and I get (for example) :

  The name 'ExtendedInfoHtmlHelpers' does not exist in the current context

An example of part of one of the global helper views (App_Code/ExtendedInfoHtmlHelpers.cshtml):

@using System.Web.Mvc
@using System.Web.Mvc.Html
@using Shift6.Dashboard.ViewModels.Sessions
@using Shift6.Extensions
@using Shift6.Web
@using Shift6.Web.Extensions

@model Shift6.Dashboard.ViewModels.Sessions.SessionInfoViewModel

@helper Title(SessionInfoViewModel Model)
{
    <div id="extended-title" class="sessions-extended-title">

        @{
            var userNameString = string.IsNullOrEmpty(Model.ClientInfo.User.AppUserId) ? "Anonymous User #" : "User #";

            var appUserIdHeader = userNameString + Model.ClientInfo.User.UserAppIndex +
                                  (!string.IsNullOrEmpty(Model.ClientInfo.User.AppUserId) ? " - " + Html.SafeRaw(Model.ClientInfo.User.AppUserId) : string.Empty);

        }

        @if (LoggedUserIdentity.Current.IsUsersEnabled)
        {
            <a class="extended-info-app-user-id long-text" title="@appUserIdHeader">
                @appUserIdHeader
                <i class="appsee-icon_search"></i>
            </a>
        }
        else
        {
            <span class="extended-info-app-user-id" title="@appUserIdHeader">@appUserIdHeader</span>
        }

    </div>
}

Example of how I'm trying to use those helpers (Views/Sessions/ExtendedInfo-Web.cshtml):

@using Newtonsoft.Json
@using Shift6.Model.Extensions
@using Shift6.Web.Extensions
@model Shift6.Dashboard.ViewModels.Sessions.SessionInfoViewModel


@ExtendedInfoHtmlHelpers.Title(Model)

<div id="sessions-extended-bottom-grid">

    <div id="sessions-extended-client-info" session-id="@Model.SessionId">

        @ExtendedInfoHtmlHelpers.SharedClientInfoLines(Model)

        <div class="client-info-line platform-info">
            @if (Model.ClientInfo.BrowserType != null)
            {
                <div class='browser-details'></div>
            }
        </div>

        @ExtendedInfoHtmlHelpers.Tags(Model)
    </div>

    @ExtendedInfoHtmlHelpers.SessionTimeline(Model)

    @ExtendedInfoHtmlHelpers.SessionExtras(Model)
</div>

Build Action on those razor files is set to "Content".

I Tried adding:

@using Shift6.Dashboard.App_Code.ExtendedInfoHtmlHelpers

to the start of each view but then I get:

A 'using namespace' directive can only be applied to namespaces; 'ExtendedInfoHtmlHelpers' is a type not a namespace. Consider a 'using static' directive instead
The type or namespace name 'ExtendedInfoHtmlHelpers' could not be found (are you missing a using directive or an assembly reference?)

Also tried adding:

@using Shift6.Dashboard.App_Code

to the start of each view but then I get:

An object reference is required for the non-static field, method, or property 'ExtendedInfoHtmlHelpers.Title(SessionInfoViewModel)'

Any help would be appreciated. And if this cannot be done I'd love to hear what in your opinion are my alternatives : )

Upvotes: 3

Views: 380

Answers (0)

Related Questions