Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a reference to a view in ASP.NET MVC

I am adding a control library reference from Infragistics to use their controls in my ASP .NET MVC 3 Razor views. I have added the dll as a reference to the project already.

reference
reference properties

My controllers can access the namespace fine if I add a using Infragistics.Web.Mvc at the top of the file.

There are two ways to tell the views to include a namespace. The first is to put @using Infragistics.Web.Mvc at the top of your view. However if I try that, I get the following error:

The type or namespace name 'Infragistics' could not be found (are you missing a using directive or an assembly reference?)

The second is to add a call to the namespace in the pages part of the view Web.config like this:

<system.web.webPages.razor>
   <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=ABCABCABCABCABC" />
   <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
         Other namespaces...
         <add namespace="Infragistics.Web.Mvc"/>
      </namespaces>
   </pages>
</system.web.webPages.razor>

however this doesn't work either.

The only way I could get it to work was by adding the following line into the root Web.config file:

<system.web>
   <compilation debug="true" targetFramework="4.0">
      <assemblies>
         ...
         <add assembly="Infragistics.Web.Mvc, Version=3.11.1.2010, Culture=neutral, PublicKeyToken=blahblahblah" />
      </assemblies>
   </compilation>

On some further investigation regarding Web.config, I found that by including that line it tells the compiler to use that resource when compiling the ASP .NET resources. I had to use SN.exe to find the PublicKeyToken.

What am I doing wrong?

like image 427
dnatoli Avatar asked Aug 19 '11 04:08

dnatoli


3 Answers

I'm not sure how the Infragistics installation works or how you're running your website, but you may need to tell Visual Studio to copy the dll on build so it's included with the web project. It's worth checking before exploring too many other options.

Here is an example

enter image description here

like image 77
Timothy Strimple Avatar answered Oct 18 '22 12:10

Timothy Strimple


Looking at your code does not seems that you have put in the web.config the following:

<pages>
  <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" />

    ...

    <add namespace="Infragistics.Web.Mvc" />
  </namespaces>
</pages>

This tell your viewengine (razor or the others) to use also the infragistics dll as an extension.

If you are using Razor look also for the "namespace" tag inside View\web.config. You can specificy the namespace there if you prefer.

Hope it helps

like image 37
Iridio Avatar answered Oct 18 '22 13:10

Iridio


Unfortunately you have something weird going on here - as the steps of simply adding the ref and including the namespace are enough to get this working.

In my apps, I have no entry in the assemblies element in the web.config and only a @using (for instance MVC grid @using MvcContrib.UI.Grid ) and everything works fine. I know that doesn't help much in resolving the issue but at least you know this 'should' work like this.

You also don't need the semicolon after your using statement. When you added the ref to that assembly which location did you add it from?

like image 28
Adam Tuliper Avatar answered Oct 18 '22 13:10

Adam Tuliper