简体   繁体   English

如何构造返回JSON对象的服务器端数据源?

[英]How to structure a server-side datasource that returns JSON objects?

I'm currently new into client-side programming, especially using the Jquery Ajax method to call server-side methods. 我目前是客户端编程的新手,尤其是使用Jquery Ajax方法调用服务器端方法。

Until now I've used a System.Web.Services.WebService to host multiple methods to return a String or a bool. 到目前为止,我已经使用System.Web.Services.WebService托管多个方法来返回String或bool。 This approach has the advantage, that I can "group" methods with a similar purpose, like "methods for a comment-system". 这种方法的优势在于,我可以将具有类似目的的方法“分组”,例如“注释系统的方法”。 A disadvantage, as I've read many times, is that *.asmx services are outdated and the Json support isn't that well. 我已经读过很多遍了,缺点是* .asmx服务已经过时,而且Json支持也不是很好。

I've always read that a good way to provide methods that return JSON objects is the use of generic handlers (*.ashx). 我一直读到,提供返回JSON对象的方法的好方法是使用通用处理程序(* .ashx)。 Generic handlers that I've written in the past provided only one single "action" per handler. 我过去编写的通用处理程序每​​个处理程序仅提供一个“操作”。 Is using generic handlers for a JSON datasource the way to go? 使用通用处理程序处理JSON数据源是否可行? I clearly see the disadvantage of not being able to "group" methods, because each method would be in a seperate .ashx.cs file. 我清楚地看到了无法对方法进行“分组”的缺点,因为每种方法都位于单独的.ashx.cs文件中。 (I know that I could pass a second argument that determines which "action" should be called, but somehow doesn't feel right). (我知道我可以传递第二个参数来确定应调用哪个“动作”,但感觉不正确)。

Using a WCF service is not a worthwhile option at the moment, because the methods that I use need access to the session and adjusting a service to fit the same purpose is just an overkill for now. 目前,使用WCF服务并不是一个值得的选择,因为我使用的方法需要访问会话并调整服务以达到相同的目的,这只是目前的矫kill过正。

I guess the real question is: What is a modern way to return JSON objects / List of JSON objects while maintaining readability / structure of the service-side methods? 我想真正的问题是: 在保持可读性/服务端方法的结构的同时,返回JSON对象/ JSON对象列表的现代方法什么?

Edit : I'm using Webforms 4.0 编辑 :我正在使用Webforms 4.0

I'd stick with WebServices I've never had problems with the JSON being returned from them. 我会坚持使用WebServices,而从它们返回的JSON从来没有问题。 using ajax with jQuery is simple also 在jQuery中使用ajax也很简单

    $.ajax({
        type: "POST",
        url: "/Methods/Zipcodes.asmx/GetPOIMarkersFrontendByTypeAndZip",
        data: "{latlng: '" + lat + "," + lng + "', type: '" + type + "', distance: '" + distance + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(html) {
        }
    }

is a brief example of passing data to a webservice 是将数据传递到Web服务的简短示例

    [WebMethod]
    public object GetPOIMarkersFrontendByTypeAndZip(string latlng, string type, string distance)
    {
        var db = new SQLConnectionDataContext();

        string[] zip = latlng.Split(',');
        IQueryable<POI> points = db.POIs;
        int dist = string.IsNullOrEmpty(distance) ? 0 : Convert.ToInt32(distance);

        List<POI> poi = type == "0"
                            ? points.ToList()
                            : points.Where(p => p.poiTypeId == Convert.ToInt32(type)).ToList();

        return (from item in poi
                where !string.IsNullOrEmpty(item.Lat) && !string.IsNullOrEmpty(item.Lng)
                let dDist =
                    DoCalc(Convert.ToDouble(zip[0]), Convert.ToDouble(item.Lat.Trim()), Convert.ToDouble(zip[1]),
                           Convert.ToDouble(item.Lng.Trim()))
                where dDist <= dist
                select new GoogleMapMarker
                           {
                               lat = item.Lat,
                               lng = item.Lng,
                               data = FpsFunctions.IsLanguageFrench() ? item.fr : item.gb,
                               tag = item.poiTypeId.ToString()
                           }).ToList();
    }

this would return a JSON array with 4 variables for me to use. 这将返回一个带有4个变量的JSON数组供我使用。 Not once had problems and also it allows for you to populate html sites with data on the fly like ASP.NET allows. 不仅没有问题,而且它还允许您像ASP.NET一样动态地用数据填充html网站。 Just getting you're head round jQuery :) 只是让您转而使用jQuery :)

I have moved onto MVC, which makes this sort of thing a lot easier. 我进入了MVC,这使这种事情变得容易得多。 But when I did this in WebForms, I would use one or more handlers (ASHX) that took in an action parameter. 但是,当我在WebForms中执行此操作时,我将使用一个或多个带有操作参数的处理程序(ASHX)。 The action parameter would then be evaluated against to call the appropriate target method which would handle the response. 然后将评估action参数以调用将处理响应的适当目标方法。

If you go this route, you're saving yourself the headache of all those files. 如果您走这条路,就可以避免所有这些文件的麻烦。 And what you lose in having to specify an action parameter on your ajax calls, you gain in not having to specify different file names. 而且,您不必在ajax调用中指定一个动作参数而失去的东西,就是不必指定其他文件名。

I would suggest looking at Web API 我建议看一下Web API

http://www.asp.net/web-api http://www.asp.net/web-api

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API是一个框架,可轻松构建可访问包括浏览器和移动设备在内的广泛客户端的HTTP服务。 ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework. ASP.NET Web API是在.NET Framework上构建RESTful应用程序的理想平台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM