简体   繁体   English

ASP.NET:用于简单ajax调用的Web服务还是WCF?

[英]ASP.NET: Web Service or WCF for simple ajax call?

I just want to simply call a method on my web service via ajax and have it return a value. 我只想简单地通过ajax在我的Web服务上调用一个方法,并让它返回一个值。

Should I use "WCF Service" , "AJAX-Enabled WCF Service" , or "Web Service" 我应该使用“ WCF服务”,“启用AJAX的WCF服务”还是“ Web服务”

Which is the easiest? 哪个最简单?

Use a generic HTTP handler instead. 请改用通用HTTP处理程序。 They are simpler to code. 它们更易于编码。

You should use Ajax-Enabled WCF service. 您应该使用启用了Ajax的WCF服务。 I don't remember the name exactly but it should be marked with an attribute to be accessible from JS. 我不记得确切的名称了,但是它应该标记一个属性,以便可以从JS访问。

除非维护现有代码且不能更改,否则永远不要使用“ Web服务”模板。

If you are just calling a single method use ScriptMethod 如果您只是调用单个方法,请使用ScriptMethod

You can code it inline with the page that it is used on. 您可以在使用它的页面上内联编码。 http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-web-services http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-web-services

Using the ScriptMethod Attribute 使用ScriptMethod属性

The ScriptService attribute is the only ASP.NET AJAX attribute that has to be defined in a .NET Web Service in order for it to be used by ASP.NET AJAX pages. ScriptService属性是必须在.NET Web服务中定义的唯一ASP.NET AJAX属性,ASP.NET AJAX页才能使用它。 However, another attribute named ScriptMethod can also be applied directly to Web Methods in a service. 但是,另一个名为ScriptMethod的属性也可以直接应用于服务中的Web方法。 ScriptMethod defines three properties including UseHttpGet, ResponseFormat and XmlSerializeString. ScriptMethod定义了三个属性,包括UseHttpGet,ResponseFormat和XmlSerializeString。 Changing the values of these properties can be useful in cases where the type of request accepted by a Web Method needs to be changed to GET, when a Web Method needs to return raw XML data in the form of an XmlDocument or XmlElement object or when data returned from a service should always be serialized as XML instead of JSON. 在以下情况下,更改这些属性的值可能很有用:需要将Web方法接受的请求类型更改为GET,或者Web方法需要以XmlDocument或XmlElement对象的形式返回原始XML数据时,从服务返回的值应始终序列化为XML而不是JSON。

The UseHttpGet property can be used when a Web Method should accept GET requests as opposed to POST requests. 当Web方法应接受GET请求而不是POST请求时,可以使用UseHttpGet属性。 Requests are sent using a URL with Web Method input parameters converted to QueryString parameters. 使用带有将Web方法输入参数转换为QueryString参数的URL发送请求。 The UseHttpGet property defaults to false and should only be set to true when operations are known to be safe and when sensitive data is not passed to a Web Service. UseHttpGet属性默认为false,仅当已知操作安全且未将敏感数据传递到Web Service时才应将其设置为true。 Listing 6 shows an example of using the ScriptMethod attribute with the UseHttpGet property. 清单6显示了将ScriptMethod属性与UseHttpGet属性一起使用的示例。

If those are your only options, I've found the AJAX-Enabled WCF Service to be the simplest to work with. 如果只有这些,我发现启用AJAX的WCF服务最简单。 It's still WCF, but it templates out for you the proper web.config setup and ditches the interface that the plain "WCF Service" template gives you. 它仍然是WCF,但它为您模板化了正确的web.config设置,并断开了普通“ WCF服务”模板为您提供的界面。 It seems to be the closest thing in the whole WCF mess to the old ASMX-style as far as being dirt simple to get going. 就整个WCF混乱而言,这似乎是最接近旧ASMX风格的最接近的事情,因为它很容易上手。

Just as another alternative, if you happen to be able to use ASP.NET MVC in your webforms project and just need this for an ajax call, you could skip the web service hoopla altogether and create a simple JSON result for your AJAX call like so: 作为另一种选择,如果您恰好能够在Webforms项目中使用ASP.NET MVC,并且只需要使用它进行Ajax调用,则可以完全跳过Web服务hoopla并为您的AJAX调用创建一个简单的JSON结果,如下所示:

// put this method in a controller
public JsonResult AjaxJsonTest(string who) {
   var result = new {
      Success = true,
      Message="Hello, " + (who ?? "world!")
   }; 
   return Json(result, JsonRequestBehavior.AllowGet);
}

And then you can call it from jQuery like this: 然后可以从jQuery这样调用它:

<script language="javascript" type="text/javascript">
function AjaxTestClick() {
    $.ajax({
        type: "POST",
        url: '<%: Url.Action("AjaxJsonTest", "Test") %>',
        data: { who: 'whomever' },
        success: function (resultData) {
            if (resultData.Success) {
                alert(resultData.Message);
            }
            else {
                alert('Ajax call failed.');
            }
        }
    });
}
</script>

Tons of options - pick what suits your situation best. 众多选择-选择最适合您的情况。

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

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