我一直在关注此处的教程Displaying Data in a Chart with ASP.NET Web Pages (Razor)
我尝试了标题为在网页中显示图表的部分,其代码为
<img src="_DrawChart.cshtml" />
但我得到了例外
A public action method '_DrawChart.cshtml' was not found on controller 'NonContractual.Areas.Reports.Controllers.HomeController'.
啊哈!我想。我需要忽略路线,所以在RouteConfig.RegisterRoutes
我添加了
routes.IgnoreRoute("{*charting}", new { charting = @"(.*/)?_DrawChart.cshtml(/.*)?" });
这是对通常的图标的简单改编。但我仍然有同样的例外。
更多的挖掘和反复试验导致我添加了忽略
的整个路线routes.IgnoreRoute("Reports/Home/_DrawChart.cshtml");
但我仍然得到同样的例外。所以我发现了这个问题/答案How to Ignore route for a specific View folder or a sepecific cshtml file in ASP.NET MVC 4所以我尽职尽责地更改了web.config文件,现在我得到了异常
Exception of type 'System.Web.HttpException' was thrown.
无论如何,我不喜欢允许直接访问.cshtml
文件的想法。
我的_DrawChart.cshtml
文件只包含教程中的代码
@{
var myChart = new Chart(width: 600, height: 400)
.AddTitle("Chart Title")
.AddSeries(
name: "Employee",
xValue: new[] { "Peter", "Andrew", "Julie", "Mary", "Dave" },
yValues: new[] { "2", "6", "4", "5", "3" })
.Write();
}
所以我的问题是,如何在.NET Web应用程序中使用包含图表绘制代码的.cshtml
文件作为src
标记的img
?
顺便说一句,我可以通过调用控制器上的方法来显示图表
<img src="@Url.Action("DrawPieChart", "Home")" />
我在这里发现的技术MVC3, Razor and MS Chart Controls in .NET 4
使用图表的信息非常少。