Johannes Setiabudi
Johannes Setiabudi

Reputation: 2077

Displaying ASP.NET Chart controls in ASP.NET MVC site with virtual path

I have a simple project that combines ASP.NET MVC with ASP.NET Charts control. Code is super simple and works IF I do not specify any "Virtual Path" when I run it in VS 2008. I followed Mike Ceranski post on his blog here: http://www.codecapers.com/post/Build-a-Dashboard-With-Microsoft-Chart-Controls.aspx

But, if I put a virtual path (in the "Web" tab in project properties), it will fail and produce this error: Failed to map the path '/ChartImg.axd'. So it seems that it is still looking to call ChartImg.axd in the root directory instead of inside the virtual path.

So, my question is - how do I get it to go to the virtual path instead?

I have also done where the controller's action just return the image filestream - which I don't want - since eventually I want to be able to make the chart clickable instead of just a plain image.

Here is my web.config settings related to ASP.NET Chart:

<appSettings>
    <add key="ChartImageHandler" value="storage=file;timeout=20;URL=/App_Data/MicrosoftChartControls/"/>
</appSettings>

<httpHandlers>
    ...
    <add verb="*" path="ChartImg.axd" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>

Code for the controller:

public ActionResult Index() {
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    ViewData["Chart"] = BindChartData();
    return View();
}

private Chart BindChartData() {
    Chart chart = new Chart();
    chart.Width = 150;
    chart.Height = 300;
    chart.Attributes.Add("align", "left");            

    chart.Titles.Add("MY CHART");  
    chart.ChartAreas.Add(new ChartArea());

    chart.Series.Add(new Series());
    chart.Legends.Add(new Legend("MY CHART"));
    chart.Legends[0].TableStyle = LegendTableStyle.Auto;
    chart.Legends[0].Docking = Docking.Bottom;

    for (int i = 0; i < 10; i++) {
        string x = ChartTest.Models.Utility.RandomText();
        decimal y = ChartTest.Models.Utility.RandomNumber(1, 100);
        int ptIdx = chart.Series[0].Points.AddXY(x, y);
        DataPoint pt = chart.Series[0].Points[ptIdx];
        pt.LegendText = "#VALX: #VALY";
    }

    chart.Series[0].Legend = "MY CHART";
    return chart;
}

Code for the aspx:

<%         
    supportChart.Controls.Add(ViewData["Chart"] as Chart);        
%>
<asp:Panel ID="supportChart" runat="server"></asp:Panel>

Upvotes: 2

Views: 1608

Answers (2)

a_hardin
a_hardin

Reputation: 5260

In order to get a path to be ignored, you have to add the specific path to the IgnoreRoute.

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

I was also able to get it to work in my particular case using:

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

Upvotes: 1

Paddy
Paddy

Reputation: 33867

I'm no routing expert, but you might need something like this in your global.asax:

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

Upvotes: 0

Related Questions