当前,我正在编写一个Web应用程序,以从数据库中提取数据并使用Google图表动态显示。当我启动网站时,我在尝试发布时收到错误消息,并留下以下错误消息:
这是我的代码:
这是.aspx文件:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="GoogleChart.aspx.cs" Inherits="GoogleChartsWebForm.GoogleChart" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
<script src="Scripts/jquery-3.3.1.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script>
var chartData; //Global Var For Data
google.load("visualization", "1", { packages: ["corechart"]});
//Fill Chart Data
$(Document).ready(function () {
$.ajax({
url: "GoogleChart.aspx/GetChartData",
data: "",
dataType: "json",
type: "POST",
contentType: "application/json; chartset=utf-8",
success: function (data) {
chartData = data.d;
},
error: function () {
alert("Error loading Data! Please Try again!");
}
}).done(function () {
//after complete loading data
drawChart();
});
});
function drawChart() {
var data = google.visualzation.arrayToDataTable(chartData);
var options = {
title: "Chart",
pointSize: 5
};
var lineChart = new google.visualization.LineChart(document.getElementById('chart_div'));
lineChart.draw(data, options);
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="chart_div" style="width: 800px; height: 400px;">
<%-- GoogleCharts WebForm Will Load Here --%>
</div>
</asp:Content>
这是.aspx.cs文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace GoogleChartsWebForm
{
public partial class GoogleChart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static object[] GetChartData()
{
List<Table> data = new List<Table>();
//Here DB ENTITY is our DB context
using(DataBaseEntities dc = new DataBaseEntities())
{
data = dc.Tables.ToList();
}
var chartData = new object[data.Count + 1];
chartData[0] = new object[]
{
"Month",
"TotalSales"
};
int j = 0;
foreach (var i in data)
{
j++;
chartData[j] = new object[] { i.Month, i.TotalSales };
}
return chartData;
}
}
}
我看不到有任何问题。我认为该错误可能是在尝试访问数据库时出现的。我对ASP.Net相当陌生,因此向您寻求帮助。对不起,这似乎是个问题。
答案 0 :(得分:0)
要解决此问题,我只是这样做了:
在~/App_Start/RouteConfig.cs
内部进行的更改:
settings.AutoRedirectMode = RedirectMode.Permanent;
收件人:
settings.AutoRedirectMode = RedirectMode.Off;