使用虚拟路径在 ASP.NET MVC 站点中显示 ASP.NET 图表控件

发布于 2024-08-22 02:21:35 字数 2249 浏览 6 评论 0原文

我有一个简单的项目,将 ASP.NET MVC 与 ASP.NET Charts 控件相结合。代码非常简单,如果我在 VS 2008 中运行它时不指定任何“虚拟路径”,它就可以工作。我在他的博客上关注了 Mike Ceranski 的帖子:http://www.codecapers.com/post/Build-a-Dashboard-With-Microsoft-Chart-Controls。但是

,如果我放置虚拟路径(在项目属性的“Web”选项卡中),它将失败并产生此错误:无法映射路径“/ChartImg.axd”。所以看起来它仍然希望在根目录中而不是在虚拟路径中调用 ChartImg.axd 。

所以,我的问题是 - 如何让它转至虚拟路径?

我还完成了控制器的操作仅返回图像文件流的操作 - 这是我不想要的 - 因为最终我希望能够使图表可单击,而不仅仅是普通图像。

这是我与 ASP.NET Chart 相关的 web.config 设置:

<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>

控制器代码:

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;
}

aspx 代码:

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-08-29 02:21:35

为了获得要忽略的路径,您必须将特定路径添加到 IgnoreRoute。

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

我还可以使用以下方法让它在我的特定情况下工作:

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

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}");
一向肩并 2024-08-29 02:21:35

我不是路由专家,但您可能在 global.asax 中需要类似的内容:

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

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

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文