使用虚拟路径在 ASP.NET MVC 站点中显示 ASP.NET 图表控件
我有一个简单的项目,将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了获得要忽略的路径,您必须将特定路径添加到 IgnoreRoute。
我还可以使用以下方法让它在我的特定情况下工作:
In order to get a path to be ignored, you have to add the specific path to the IgnoreRoute.
I was also able to get it to work in my particular case using:
我不是路由专家,但您可能在 global.asax 中需要类似的内容:
I'm no routing expert, but you might need something like this in your global.asax: