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 no开发者_开发知识库t 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?
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
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:
- Discussion: http://forums.asp.net/t/1391446.aspx/1?MS+Chart+System+Web+DataVisualization+not+found
- Solution: http://social.msdn.microsoft.com/Forums/en/MSWinWebChart/thread/d976b659-264a-4731-935c-08c02865002f
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
Did you reference the dll in your webconfig? Sometimes Visual Studio gets confused if you are not explicit.
精彩评论