سؤال

Recently I upgraded an ASP.NET WebForms project to be an MVC hybrid.

It broke our ASP.NET Chart Control however - the images were no longer rendering in reports.

Upgrading from 3.5 to 4.0 of the chart control could also be the problem. I replaced references from locally copied 3.5 assemblies to the 4.0 GAC version of System.Web.UI.DataVisualization.Charting assembly.

هل كانت مفيدة؟

المحلول

There were several steps to getting this working again

1) Here are the relevant snippets of the web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <system.web>
      <pages>
         <controls>
            <add tagPrefix="asp" namespace="System.Web.UI.DataVisualization.Charting" assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
         </controls>
      </pages>
      <compilation defaultLanguage="c#" targetFramework="4.5">
         <assemblies>
            <add assembly="System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
         </assemblies>
      </compilation>
   </system.web>
   <system.webServer>
      <handlers>
         <remove name="ChartImageHandler" />
         <add name="ChartImageHandler" preCondition="integratedMode" verb="GET,HEAD,POST" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </handlers>
   </system.webServer>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="System.Web.DataVisualization" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="4.0.0.0" />
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

2) I needed to ignore the route of the axd. The url requesting the chart image was of the form http://sitename/reports/chartimg.axd?i=chart444334&i=0

So I had to be careful to include that /reports/ url in the route

routes.IgnoreRoute("Reports/{resource}.axd/{*pathInfo}");

Note that this required placing at the top of the routes. It didn't work when placed at the bottom of the route config. Experts in MVC routing will probably be LDO, but it wasn't obvious to me.

3) At this point the charts were being generated but I was still getting 404 image not found when trying to serve the chart images. This implies there is still a problem in my web.config perhaps.

I gave up trying to use the HTTP Handler and switched to file based location. This is applied to the chart control itself:

ImageStorageMode = ImageStorageMode.UseImageLocation;

This is not ideal if you don't want a curious user enumerating the charts as the numbering is predictable, fortunately my charts are not sensitive enough to warrant concern.

This also means some of the web.config in step one could be removed.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top