2

I have a project on Sitecore 9.3 that uses both View and Controller renderings. My view renderings have not had any intellisense errors so far and work just fine after being built.

When I built my first Controller Rendering, I got the error "The type or namespace name 'AspNetCore' does not exist in the namespace 'Microsoft'. If I add a package reference to AspNetCore v2.2.0, the intellisense errors on my Controller Rendering are fixed; however, now I see intellisense errors on all of my View Renderings.

The error on my View Renderings is "'IHtmlHelper' does not contain a definition for 'Sitecore' and the best extension method overload 'HtmlHelperExtensions.Sitecore(HtmlHelper)' requires a receiver of type 'HtmlHelper'".

I have verified that I am using the correct version of System.Web.Mvc (5.2.4) and all references in the web.config files in the views folders are the correct version. I also verified that the version of System.Web.Mvc.dll is the same in my web root.

<?xml version="1.0"?>

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Sitecore.Mvc" />
        <add namespace="Sitecore.Mvc.Presentation"/>
        <add namespace="Sitecon.Feature.PageContent.Models"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <appSettings>
    <add key="webpages:Enabled" value="false" />
  </appSettings>

  <system.webServer>
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>

  <system.web>
    <compilation>
      <assemblies>
        <add assembly="System.Web.Mvc, Version=5.2.4.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>
</configuration>

I also tried adding a package reference to Microsoft.AspNet.Mvc (5.2.4) but that did not fix the problem.

I have verified that the version of the web.config file in the views folder in the web root is the same as the version in my project. All web.config files in my project have their build action set to none and copy to output directory set to do not copy so the web root is never overridden.

I am using the Sitecore Helix Templates VS Extension (if that makes a difference). If I publish the solution as-is with the intellisense errors, it makes no difference and my code works just fine when rendered on the live site. But, I would very much like to remove all intellisense errors from my solution. Thank you in advance for your help!

Edit: Adding Controller Code

using Sitecon.Feature.PageContent.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Sitecon.Feature.PageContent.Controllers
{
  public class PageContentController : Controller
  {
    public ActionResult ListWithIcons()
    {
      if (Sitecore.Context.Item == null)
      {
        return new EmptyResult();
      }

      var item = Sitecore.Context.Item;
      var multiLineTextString = item.Fields[Templates.ListWithIcons.Fields.ListText].Value;

      ListWithIconsItems listWithIconsItems = new ListWithIconsItems();
      listWithIconsItems.ListItems = new List<string>();

      if (string.IsNullOrEmpty(multiLineTextString))
      {
        return new EmptyResult();
      }

      string[] sep = new string[] { "\r\n" };
      string[] lines = multiLineTextString.Split(sep, StringSplitOptions.RemoveEmptyEntries);

      foreach (string listItem in lines)
      {
        if (!string.IsNullOrEmpty(listItem))
        {
          listWithIconsItems.ListItems.Add(listItem);
        }
      }

      listWithIconsItems.ListIcon = item.Fields[Templates.ListWithIcons.Fields.ListIcon].Value;

      return View(listWithIconsItems);
    }
  }
}

Model Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sitecon.Feature.PageContent.Models
{
    public class ListWithIconsItems
    {
      public string ListIcon { get; set; }
      public List<string> ListItems { get; set; }
    }
}

Controller Rendering Code:

@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@using Sitecon.Feature.PageContent.Models
@using Sitecon.Foundation.SitecoreHelperExtensions
@model ListWithIconsItems

<ul class="fa-ul">
  @foreach (var li in Model.ListItems)
  {
    <li><span class="fa-li"><i class="fas fa-@Model.ListIcon"></i></span>@li</li>
  }
</ul>

Intellisense is erroring on Model.ListItems & Model.ListIcon: The name 'Model' does not exist in the current context

Intellisense Errors

3
  • It is unlikely you need AspNetCore for a controller rendering; Sitecore runs on the regular .NET framework. You should look into, what part of your code would require such a reference.
    – Mark Cassidy
    Feb 24 '20 at 14:30
  • Can you include a sample of your controller code that is giving you the original namespace error. As Mark said, you shouldn't need any part of netcore in a regular controller rendering
    – Richard Seal
    Feb 24 '20 at 14:42
  • Thanks guys, I added all relevant code. I'm sure I'm overlooking something simple! Feb 25 '20 at 12:42
2

If you have upgraded to the new project type (e.g. Project Sdk="Microsoft.NET.Sdk") you'll need to add the Microsoft.AspNetCore.Mvc NuGet package. This should remove any errors regarding the AspNetCore error in Visual Studio

2

This has finally been solved.

Add

<ItemGroup>
    <ProjectCapability Include="SupportsSystemWeb" />
</ItemGroup> 

to the .csproj file

Tested and works in Visual Studio 2019 16.8.3

The example templates for the Visual Studio Helix extension will be updated with this fix asap.

1
  • I was about to post this and credit Anders, but here's the man himself, and I confirm it works. Make the change! Jan 11 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.