CSHTML not recognizing WebViewPage or T or Html in custom Plugin

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Hi,

I'm creating a custom plugin against Nop 3.9.  I'm using Visual Studio Community 2017 Edition.  I created a class library using .NET Framework 4.5.1 and using NuGet I added "Microsoft.AspNet.Mvc" and all other packages.  Basically, I'm duplicating an existing plugin.

I copied the web.config file from another plugin and pasted it into my plugin.  All the references in my plugin are there including System.Web.Mvc version 5.2.3.  However, my CSHTML files are not recognizing WebViewPage variable.  It's showing the red squiggly line under it.  This is, however, working on existing plugins.

I have a feeling that I need to hack something to get this working because apparently using NuGet is not enough.

Please, can anyone tell me what I'm doing wrong?  This has taken 4 days of my time trying to get this thing to work with no success.  Please see below for details.  I have cleaned my plugin, rebuilt it, cleaned the Nop solution, rebuilt it but nothing works.

Here's my web.config file at the Class Library (Plugin) level.


<?xml version="1.0" encoding="utf-8"?>
<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>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation targetFramework="4.5.1" />
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
  </system.web>
  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="Nop.Web.Framework.ViewEngines.Razor.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
</configuration>



Here's the code giving error in my cshtml.  The bold is not recognized and errors out.


@model MyListModel
@{
    var defaultGridPageSize = EngineContext.Current.Resolve<AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<AdminAreaSettings>().GridPageSizes;

    //page title
    WebViewPage.ViewBag.Title = T("Admin.Affiliates").Text;
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Affiliates");
}


Here's my packages file:


<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Autofac" version="4.4.0" targetFramework="net451" />
  <package id="FluentValidation" version="6.4.1" targetFramework="net451" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net451" />
  <package id="Microsoft.Bcl" version="1.1.10" targetFramework="net451" />
  <package id="Microsoft.Bcl.Async" version="1.0.168" targetFramework="net451" />
  <package id="Microsoft.Bcl.Build" version="1.0.21" targetFramework="net451" />
  <package id="Microsoft.Net.Http" version="2.2.29" targetFramework="net451" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" />
</packages>


On my plugin project, the following libraries are referenced:


Autofac
Autofac.Integration.Mvc
EntityFramework
EntityFramework.SqlServer
FluentValidation
Microsoft.CSharp
Microsoft.Threading.Tasks
Microsoft.Threading.Tasks.Extensions
Microsoft.Threading.Tasks.Extensions.Desktop
Microsoft.Web.Infrastructure
Nop.Admin
Nop.Core
Nop.Data
Nop.Services
Nop.Web
Nop.Web.Framework
System
System.ComponentModel.DataAnnotation
System.Core
System.Data
System.data.DataSetExtensions
System.Net
System.Net.Http
System.Net.Http.Extensions
System.Net.Http.Primitives
System.Web
System.Web.Helpers
System.Web.Mvc
System.Web.Razor
System.Web.WebPages
System.Web.WebPages.Deployment
System.Web.WebPages.Razor
System.Xml
System.Xml.Linq


Thank you for any help or guidance.  I've also reset my VS settings, cleared its cache.
6 years ago
Make sure web.config copied to the output path.

For this, you have go properties(right click on this file) of this file and then select

Copy to output directory ==> Copy if newer
6 years ago
Hi Sohel,
Thank you for your reply.  I had the Copy Local set to True already but that didn't work.

I think I know what the problem is.  Using a Class Library, I'm trying to access variables such as ViewBag that normally should be accessed on a Website Project and not a Class Library Project.

The reason I did this was because I thought one can create a fully contained plugin for the Admin Portal with Views and all in one place but it seems that's not the case.  The Plugins are used to *only* override the logic in NopCommerce but if one wants to add a page to the Nop Admin, a folder should be created under the Nop.Admin - Views folder since it's a website project.

Is my understanding above correct?  I noticed, none of the plugins have CRUD views.
6 years ago
bpejman wrote:
Thank you for your reply.  I had the Copy Local set to True already but that didn't work.

I think I know what the problem is.  Using a Class Library, I'm trying to access variables such as ViewBag that normally should be accessed on a Website Project and not a Class Library Project.

It's just the Razor intellisense that doesn't work in Visual Studio for class library projects, there are quite a few posts about it on the forum: https://www.nopcommerce.com/boards/search.aspx?searchterms=intellisense. As long as the code is correct it should still work at runtime.

bpejman wrote:
The reason I did this was because I thought one can create a fully contained plugin for the Admin Portal with Views and all in one place but it seems that's not the case.  The Plugins are used to *only* override the logic in NopCommerce but if one wants to add a page to the Nop Admin, a folder should be created under the Nop.Admin - Views folder since it's a website project.

Is my understanding above correct?  I noticed, none of the plugins have CRUD views.

No, several of the core plugins have CRUD views, see the Shipping.FixedOrByWeight plugin as an example.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.