3

I'm trying to return a chart to an MVC ActionResult as the view's Model but am hitting the following error:

CS0012: The type 'System.Web.UI.DataVisualization.Charting.Chart' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

The project I'm writing is in MVC3, using Razor as the front-end markup (which shouldn't make any difference, right?). I've included the following declarations in my Web.Config

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
      <handlers>
          <!-- Microsoft Chart Controls -->
          <add name="ChartImg" path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
          <add name="ReportViewerWebControl" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler,     Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"   />
      </handlers>
  </system.webServer>

My ActionResult code is pretty vanilla:

[HttpGet]
public ActionResult Visits()
{
    StatModel model = new StatModel();
    return View(model);
}

And the view in question looks like this:

@foreach (Chart chart in Model.ColumnCharts)
{ 
    @chart
}

From what I'm reading about the exception being returned, the problem is that the Chart type isn't being picked up correctly by the view when it comes to rendering the image, but the System.Web.DataVisualisation assembly appears in my project references (v. 4.0.0.0). What else should I be looking at?

2
  • Have you added this namespace to Web.config in View folder <add namespace="System.Web.UI.DataVisualization.Charting" /> ? Commented Aug 31, 2011 at 8:01
  • Yes, I had. Unfortunately I no longer work for the company for which this was an issue and never got around to resolving it. Commented Aug 31, 2011 at 8:24

3 Answers 3

3
+50

Is the type System.Web.UI.DataVisualization.Charting.Chart picked up from the GAC? It sometimes fails to load some assemblies from the GAC sometimes. Please add the corresponding dll in your bin directory directly and try running your app and see if that works out for you.

The problem might be due to the fact that you are referencing the type in your view and not in your controller, in that case you probably need to define the assembly info as a web.config setting or as a page directive.

<assemblies>
        <add assembly="System.Web.UI.DataVisualization.Charting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=<YourPublicKeyToken>(31bf3856ad364e35)"/>   
</assemblies>

Hope that fixes things

Sign up to request clarification or add additional context in comments.

Comments

2

Try to make the CopyLocal value in properties of the reference set to true, although not sure will help.

It might be a permissions problem.

It sounds like the Chart installer needs to be installed on any machine that tries to use it, and also has to run in full trust to be allowed to load the assembly.

At least this is what seems to be said in some place, like:

You might be able to work around this as in:
http://www.tugberkugurlu.com/archive/asp-net-chart-control-on-shared-hosting-environment-chartimagehandler-parser-error-problem-solution

Comments

1

Did you reference the dll in your webconfig? Sometimes Visual Studio gets confused if you are not explicit.

6 Comments

No I haven't, but how do I get the PublicKeyToken value?
Never mind, it's the same as what's referenced in the Handlers section of course!
Ok, the next problem then is that I'm getting a NullReferenceException on this line of the view: @foreach (System.Web.UI.DataVisualization.Charting.Chart chart in Model.ColumnCharts)
It doesn't appear to even hit the controller when this exception is raised. Break points aren't reached. So what could be null?
Ah. It's the Model. But it isn't getting as far as the controller.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.