从ASPNET控制器发送数据作为JSON

问题描述 投票:2回答:1

所以,我试图建立使用C#中的highchartJs。我从一些模型和方法(不使用它的背库)建设的图表,然后尝试提取它从js文件,其中装载的highchartJs数据。

我试图从API为JSON发送数据,这样我就可以dinamically读取(数据来自数据库查询后),但我似乎无法找到一种方法把它作为JSON

这是我迄今为止

这是我想通过从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
                                }
                            }
                        }]
                    }
                });
            });
        });
c# asp.net json
1个回答
2
投票

您需要托管您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端口;一般来说,你应该读什么是API和的WebAPI从MSDN文档开始。

© www.soinside.com 2019 - 2024. All rights reserved.