ASP.NET AJAX:如何使用Session获取JSON响应?

时间:2015-01-08 08:42:38

标签: c# asp.net ajax json session

我是ASP.NET的新手,但我要快速而有力地处理它(我知道我在问天空,但我必须;))。 我有一个页面,使用谷歌地图显示许多标记。所以我想以异步方式向服务器询问坐标。我认为最好的方法是通过AJAX通过json序列化并通过ASP会话跟踪已经发送到客户端的地图部分并仅发送新的。 我在许多方面尝试过,但我总是发现破坏魔法的问题。

我的JS代码是:

function map2Json() {
    return JSON.stringify({ "categories": $("#typeSelector input:checked").map(function () {
                                        return $(this).val();
                                    }).get(),
                            "bounds": boundsDecorator(map.getBounds())
                        })
};
function getMarkers() {
    $.ajax({
        type: "POST",
        url: "GetMarkers",
        data: map2Json(),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: OnFailure
    });
}

虽然我的ASP.NET C#代码( WebService.cs )是:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetMarkers(List<string> categories, Bounds bounds)
{
    System.Diagnostics.Debug.WriteLine(categories.ToString());
    System.Diagnostics.Debug.WriteLine(bounds.SouthWest.lat);

    return JsonConvert.SerializeObject(getMarkers(categories, bounds));
}

private string getRectangle(Bounds bounds)
{
    return string.Format("polygon(({0} {1}, {2} {1}, {2} {3}, {0} {3}, {0} {1}))", bounds.NorthEast.lng.ToString(CultureInfo.InvariantCulture), bounds.NorthEast.lat.ToString(CultureInfo.InvariantCulture), bounds.SouthWest.lng.ToString(CultureInfo.InvariantCulture), bounds.SouthWest.lat.ToString(CultureInfo.InvariantCulture));
}
private string STpolygon(string polygon)
{
    return string.Format("geography :: STGeomFromText('{0}', 4326)", polygon);
}
private string typologiesListString(List<string> typologies)
{
    return string.Format("({0})", string.Join(",", typologies));
}
private string formatMapNew(List<MapViewed> maps)
{
    return string.Join(" OR ", maps.Select(x => string.Format("{0}.STIntersects(GeoLocation) = 1 and Tipologia = {1}", x.lastPatch.ToString(), x.typology)));
}
private DataSet getCoordinatesInPolygon(List<MapViewed> maps)
{
    SqlCommand Command_coord = new SqlCommand();
    Command_coord.CommandType = CommandType.Text;
    SqlConnection conn = new SqlConnection("Server=localhost\\SQLEXPRESS;Database=geotagging;Trusted_Connection=Yes;Integrated Security=SSPI;");
    Command_coord.Connection = conn;

    string query = string.Format(@"
                select [Ragione sociale], GeoLocation, Tipologia, Dettagli
                from GEOTAG
                where {0}", formatMapNew(maps));

    Command_coord.CommandText = query;


    DataSet coordinatesDS = new DataSet();
    SqlDataAdapter coordinatesDA = new SqlDataAdapter();
    coordinatesDA.SelectCommand = Command_coord;
    try
    {
        coordinatesDA.Fill(coordinatesDS);
    }
    catch (System.Data.SqlClient.SqlException e)
    {
        System.Diagnostics.Debug.WriteLine("Error query: \n{0}\n Message = {1}", query, e.Message);
    }
    return coordinatesDS;
}
private bool IsEmpty(DataSet dataSet)
{
    foreach (DataTable table in dataSet.Tables)
        if (table.Rows.Count != 0) return false;

    return true;
}
private List<Marker> getDBMarkers(DataSet coordinatesDS)
{
    List<Marker> markers = new List<Marker>();
    SqlGeography point;
    Marker marker;

    if (!IsEmpty(coordinatesDS))
    {
        foreach (DataRow row in coordinatesDS.Tables[0].Rows)
        {
            point = (SqlGeography)row[1];
            marker = new Marker((string)row[0], new Point(point.Lat.Value, point.Long.Value), (string)row[2], (string)row[3]);
            markers.Add(marker);
        }
    }
    return markers;
}
private List<Marker> getMarkers(List<string> typologies, Bounds bounds)
{
    return getDBMarkers(getCoordinatesInPolygon(updatedMaps(bounds, typologies)));
}

private List<MapViewed> updatedMaps(Bounds bounds, List<string> typologies)
{
    List<MapViewed> maps = new List<MapViewed>();
    MapViewed map;
    foreach (string typology in typologies)
    {
        if (Session["mapViewed-" + typology] == null) Session["mapViewed-" + typology] = new MapViewed(typology);
        map = (MapViewed)Session["mapViewed-" + typology];
        map.mergeAndGetDiff(getRectangle(bounds));
        maps.Add(map);
    }
    return maps;
}

请不要过分关注我的代码的丑陋。我也尝试在我的页面后面的代码中使用几乎相同的代码,但我使用的每个方法都应该是静态的,所以我不能使用Session变量。

问题

你会做什么?

注释

我没有把代码放在地图上插入标记,因为没有必要。

解决方案

谢谢大家的建议。按照你的建议我把这段代码放在后面的代码中,它运作良好:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using Microsoft.SqlServer.Types;
using System.Data.SqlTypes;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    static SqlConnection conn = new SqlConnection("Server=localhost\\SQLEXPRESS;Database=geotagging;Trusted_Connection=Yes;Integrated Security=SSPI;");

    protected void Page_Load(object sender, EventArgs e)
    {
        string[] typologies = new string[] { "Ricambi", "Concessionario", "Motocicli", "Officina" };
        foreach (string typology in typologies)
        {
            HttpContext.Current.Session["mapViewed-"+typology] = new MapViewed(typology);
        }
    }
    private static string getRectangle(Bounds bounds)
    {
        return string.Format("polygon(({0} {1}, {2} {1}, {2} {3}, {0} {3}, {0} {1}))", bounds.NorthEast.lng.ToString(CultureInfo.InvariantCulture), bounds.NorthEast.lat.ToString(CultureInfo.InvariantCulture), bounds.SouthWest.lng.ToString(CultureInfo.InvariantCulture), bounds.SouthWest.lat.ToString(CultureInfo.InvariantCulture));
    }
    private static string STpolygon(string polygon)
    {
        return string.Format("geography :: STGeomFromText('{0}', 4326)", polygon);
    }
    private string typologiesListString(List<string> typologies)
    {
        return string.Format("({0})", string.Join(",", typologies));
    }
    private static string formatMapNew(List<MapViewed> maps)
    {
        return string.Join(" OR ", maps.Select(x => string.Format("{0}.STIntersects(GeoLocation) = 1 and Tipologia = '{1}'", STpolygon(x.lastPatch.ToString()), x.typology)));
    }
    private static DataSet getCoordinatesInPolygon(List<MapViewed> maps)
    {
        SqlCommand Command_coord = new SqlCommand();
        Command_coord.CommandType = CommandType.Text;
        Command_coord.Connection = conn;
        string query = string.Format(@"
                        select [Ragione sociale], GeoLocation, Tipologia, Dettagli
                        from GEOTAG
                        where {0}", formatMapNew(maps));
        Command_coord.CommandText = query;
        DataSet coordinatesDS = new DataSet();
        SqlDataAdapter coordinatesDA = new SqlDataAdapter();
        coordinatesDA.SelectCommand = Command_coord;
        try
        {
            coordinatesDA.Fill(coordinatesDS);
        }
        catch (System.Data.SqlClient.SqlException e)
        {
            System.Diagnostics.Debug.WriteLine("Error query: \n{0}\n Message = {1}", query, e.Message);
        }
        return coordinatesDS;
    }
    private static bool IsEmpty(DataSet dataSet)
    {
        foreach (DataTable table in dataSet.Tables)
            if (table.Rows.Count != 0) return false;
        return true;
    }
    private static List<Marker> getDBMarkers(DataSet coordinatesDS)
    {
        List<Marker> markers = new List<Marker>();
        SqlGeography point;
        Marker marker;

        if (!IsEmpty(coordinatesDS))
        {
            foreach (DataRow row in coordinatesDS.Tables[0].Rows)
            {
                point = (SqlGeography)row[1];
                marker = new Marker((string)row[0], new Point(point.Lat.Value, point.Long.Value), (string)row[2], (string)row[3]);
                markers.Add(marker);
            }
        }
        return markers;
    }
    private static List<Marker> getMarkers(List<string> typologies, Bounds bounds)
    {
        return getDBMarkers(getCoordinatesInPolygon(updatedMaps(bounds, typologies)));
    }

    private static List<MapViewed> updatedMaps(Bounds bounds, List<string> typologies)
    {
        List<MapViewed> maps = new List<MapViewed>();
        MapViewed map;
        foreach (string typology in typologies)
        {
            map = (MapViewed)HttpContext.Current.Session["mapViewed-" + typology];
            map.mergeAndGetDiff(getRectangle(bounds));
            maps.Add(map);
        }
        return maps;
    }

    [System.Web.Services.WebMethod(EnableSession = true)]
    public static List<Marker> GetMarkers(List<string> categories, Bounds bounds)
    {
        return getMarkers(categories, bounds);
    }
}

2 个答案:

答案 0 :(得分:1)

使用static HttpContext.Current property访问Session变量:

HttpContext.Current.Session

答案 1 :(得分:1)

你可以在像这样的静态方法中使用session

//Store value in session
HttpContext.Current.Session["mysession"]=value;

//Get value from session
var sessionvaue=HttpContext.Current.Session["mysession"];