从aspNet控制器以JSON格式发送数据

时间:2019-02-05 18:54:43

标签: c# asp.net json

因此,我正在尝试使用c#构建highchartJs。我正在从某些模型和方法(不使用背面的库)构建图表,然后尝试从正在加载highchartJs的js文件中获取数据。

我正尝试以JSON的形式从api发送数据,因此我可以进行动态获取(数据将稍后从数据库查询中获取),但是我似乎找不到找到将其作为json传递的方法< / p>

这是我到目前为止所拥有的

这是我想从c#中作为json传递的数据

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Net;
        using System.Net.Http;
        using System.Web.Http;
        using ItauSolution01.Models;

        namespace ItauSolution01
        {
            public class MCorretivasAXAController : ApiController
            {
                Grafico grafico = new Grafico
                {
                    categories = new string[] {
                        "Média 2016"
                        , "Jan"
                        , "Fev"
                        , "Mar"
                        , "Abril"
                        , "Maio"
                        , "Jun"
                        , "Jul"
                        , "Ago"
                        , "Set"
                        , "Out"
                        , "Nov"
                        , "Dez"
                        , "Média 2017"
                    },

                    series = new Serie[] {
                        new Serie
                        {
                            name = "Abertas"
                            , data = new int[]
                            {
                               3757, 3880, 3588, 4039, 3902, 4082, 3994, 3951, 4279, 3859, 3903, 3986, 3879, 3945
                            }
                        },
                        new Serie
                        {
                            name = "Executadas",
                            data = new int[]
                            { 3757, 3880, 3588, 4039, 3902, 4082, 3994, 3951, 4279, 3859, 3903, 3986, 3879, 3945 }
                        }
                    }

                };

                // GET api/<controller>
                public Grafico Get()
                {
                    return grafico;
                }

                // GET api/<controller>/5
                public string Get(int id)
                {
                    return "value";
                }

                // POST api/<controller>
                public void Post([FromBody]string value)
                {
                }

                // PUT api/<controller>/5
                public void Put(int id, [FromBody]string value)
                {
                }

                // DELETE api/<controller>/5
                public void Delete(int id)
                {
                }
            }
        }

这是需要此数据的图

        $(document).ready(function () {
            $.getJSON("file:///C:/Users/Simonini%20Software/Desktop/Itau/ItauSolution01/ItauSolution01/Controllers/mcorretivasaxacontroller.cs")
            .done(function (response) {

                var chart = Highcharts.chart('container02', {

                    chart: {
                        type: 'column'
                    },

                    title: {
                        text: 'CORRETIVAS ABERTAS x ACUMULADO'
                    },

                    legend: {
                        align: 'right',
                        verticalAlign: 'middle',
                        layout: 'vertical'
                    },

                    xAxis: {
                        categories: [
                            'Backlog Mês Anterior'
                            , 'Executado'
                            , 'Backlog Automação'
                            , 'Backlog Elétrica','Backlog Mecânica'
                            , 'Backlog Incêndio'
                            , 'Backlog Atual'
                        ],
                        labels: {
                            x: -10
                        }
                    },

                    yAxis: {
                        allowDecimals: false,
                        title: {
                            text: ''
                        }
                    },

                    series: [{
                        name: 'DC1',
                        data: [103, 20, 59, 39, 8, 2, 108],
                        dataLabels: {
                            enabled: true,
                            rotation: -90,
                            color: '#FFFFFF',
                            align: 'right',
                            y: 10, // 10 pixels down from the top
                            style: {
                                fontSize: '13px',
                                fontFamily: 'Verdana, sans-serif'
                            }
                        }
                    }, {
                        name: 'DC2',
                        data: [181, 39, 122, 38, 25, 1, 186],
                        dataLabels: {
                            enabled: true,
                            rotation: -90,
                            color: '#FFFFFF',
                            align: 'right',
                            y: 10, // 10 pixels down from the top
                            style: {
                                fontSize: '13px',
                                fontFamily: 'Verdana, sans-serif'
                            }
                        }
                    }, {
                        name: 'NOC',
                        data: [54, 18, 41, 15, 7, 1, 64],
                        dataLabels: {
                            enabled: true,
                            rotation: -90,
                            color: '#FFFFFF',
                            align: 'right',
                            y: 10, // 10 pixels down from the top
                            style: {
                                fontSize: '13px',
                                fontFamily: 'Verdana, sans-serif'
                            }
                        }
                    }],

                    responsive: {
                        rules: [{
                            condition: {
                                maxWidth: 800
                            },
                            chartOptions: {
                                legend: {
                                    align: 'center',
                                    verticalAlign: 'bottom',
                                    layout: 'horizontal'
                                },
                                yAxis: {
                                    labels: {
                                        align: 'left',
                                        x: 0,
                                        y: -5
                                    },
                                    title: {
                                        text: null
                                    }
                                },
                                subtitle: {
                                    text: null
                                },
                                credits: {
                                    enabled: false
                                }
                            }
                        }]
                    }
                });
            });
        });

1 个答案:

答案 0 :(得分:1)

您需要在IIS服务器上托管ASP.NET Web API项目(这是一种执行应用程序的方法),然后您可以通过URL访问API-替换行

  $.getJSON("file:///C:/Users/Simonini%20Software/Desktop/Itau/ItauSolution01/ItauSolution01/Controllers/mcorretivasaxacontroller.cs")

在客户代码行中

$.getJSON('http://localhost:80/api/MCorretivasAXA')

如果使用默认的80端口,它应该可以工作。 通常,您应该从阅读MSDN文档中的API和WebAPI开始。