Razor - microsoft-web-helpers Question

Topics: Writing modules
Mar 27, 2011 at 5:57 PM
Edited Mar 27, 2011 at 5:58 PM

In my Views I want to use eg WebGrid Helper. Using Library Package Manager I added microsoft-web-helpers in Visual Studio which added the System.Web.Helpers namespace to my module-project references.
When trying to add this namespace to my View, the system doesn't recognize this namespace?

@model IEnumerable<Xyz.Models.OverviewCategories>
@using Xyz.Models;
@using Orchard.Utility.Extensions;
@using System.Web.Helpers;

@{
    var grid = new WebGrid(Model);
}
...

 

Mar 28, 2011 at 3:49 PM

The microsoft-web-helpers library that you installed into your project does not include the WebGrid component.  For a list of the add-on components installed in this library, see the documentation for the Microsoft.Web.Helpers namespace:

http://msdn.microsoft.com/en-us/library/microsoft.web.helpers(VS.99).aspx

To be able to use the WebGrid helper (which is indeed in the System.Web.Helpers namespace), you need to first install ASP.NET Web Pages with Razor syntax.   There are 2 ways to do that:  install Microsoft WebMatrix by using the WebPI, or (since you are using VS) just install MVC3 which will include VS tools so that you can use ASP.NET Web pages with Razor syntax. 

For info on how to install MVC3 and set up VS for working with the Razor syntax, see the following.  After you've installed MVC3 as described, your VS installation will "recognize" the System.Web.Helpers NS and the WebGrid helper.

http://www.asp.net/webmatrix/tutorials/program-asp-net-web-pages-in-visual-studio

 

 

Mar 28, 2011 at 4:28 PM
Edited Mar 28, 2011 at 4:28 PM

Thanks.

I added System.Web.Helpers NS to the references in my module but the system doesn't recognize this NS when adding it to my View:
@using System.Web.Helpers;

 

Mar 28, 2011 at 4:54 PM

Add the references in your Web.config as shown in the other thread under:

 <system.web.webPages.razor>

Then you shouldn't need the @using reference at all.

Mar 28, 2011 at 5:13 PM

Thanks.

you mean in this section?

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <pages pageBaseType="Orchard.Mvc.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" />
        <add namespace="System.Linq"/>
        <add namespace="System.Collections.Generic"/>
        <add namespace="Orchard.Mvc.Html"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Mar 28, 2011 at 5:53 PM

Yes. Those namespaces are all automatically referenced in your Razor views.

Mar 28, 2011 at 8:19 PM

Ok, should I add the reference in Orchard.Framework references or in the refrences of my module?

Thanks.

Mar 28, 2011 at 8:23 PM

Thanks.

I added System.Web.Helpers to my module references (and added it to my web.config), but still WebGrid isn't available?

Mar 28, 2011 at 9:02 PM

Compile your project and close/reopen Visual Studio. I find that sometimes sorts things out for no obvious reason :)

Mar 29, 2011 at 4:31 PM

This should work at this point.   Shouldn't have to do anything other than add a reference.  If, in your site or project, you added a reference by browsing to the local ASP.NET Web pages installation folder and pointing to the System.Web.Helpers assembly, then you should be able to reference System.Web.Helpers.WebGrid in your code, I double-checked it just now and it worked.  

Mar 29, 2011 at 5:59 PM

I added the reference to System.Web.Helpers to my module. Obviously something goes wrong because @using System.Web.Helpers gives an error message (can't find it). Even when adding the assembly to the module's web.config.

Mar 30, 2011 at 8:43 PM

Gdillen I see what you mean, I can repro the fact that if I sync my enlistment to latest Orchard source, build the site, and then open the "Orchard.Web" site in VS, and add a reference to the System.Web.Helpers.dll assembly from ASP.NET Web Pages, it seems like Intellisense does not want to recognize statements such as @using System.Web.Helpers; in a .cshtml file.  However, I also noticed the following items that DO work:

* In a .cs code file, I can definitely access System.Web.Helpers, and controls within the NS including the Webgrid.  So, VS does know that the assembly is there and recognizes the namespace.

* Also, in a .cshtml file, even though Intellisense doesn't want to recognize statements like the one above, I found that I could still add such statements to the file, and they would successfully build and the page works at runtime.  Have you tried this?   I found that if I add a @using System.Web.Helper;   line to a .cshtml file in the project, it builds and runs even though Intellisense doesn't pick up on it. 

This makes you wonder if we have some kind of Intellisense issue with Razor pages.  Not sure what the general problem is though, because in VS, I tried another approach, namely creating a new MVC 3 project in which I go with the default Razor view engine.  And in View pages in that new app, it recognizes System.Web.Helpers NS in Intellisense just fine.  So whatever the problem is, it seems to be unique in Orchard, it doesn't seem to apply to all MVC apps.

I pinged the developers on this question.

Mar 31, 2011 at 4:48 AM

Sorry, I thought I'd already posted this in full (there's a similar discussion in anther thread). Your module project should have web.config looking like this:

<?xml version="1.0"?>
<configuration>

  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <remove name="host" />
      <remove name="pages" />
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <pages pageBaseType="Orchard.Mvc.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" />
        <add namespace="System.Linq"/>
        <add namespace="System.Collections.Generic"/>
        <add namespace="Orchard.Mvc.Html"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <system.web>
    <compilation targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </assemblies>
    </compilation>
  </system.web>

</configuration>

Razor Intellisense works fine for me like this. You should add System.Web.Helpers into the <system.web.webPages.razor> bit (although I haven't tested System.Web.Helpers myself so maybe there's another problem going on here).

 

Jun 8, 2011 at 9:09 AM

Add System.Web.Helpers assembly ... like this

<?xml version="1.0"?>
<configuration>

  <configSections>
    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <remove name="host" />
      <remove name="pages" />
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=1.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=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    <pages pageBaseType="Orchard.Mvc.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" />
        <add namespace="System.Linq"/>
        <add namespace="System.Collections.Generic"/>
        <add namespace="Orchard.Mvc.Html"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

  <system.web>
    <compilation targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        <add assembly="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      </assemblies>
    </compilation>
  </system.web>

</configuration>