Asp.net Application 使用 JsonRPC 调用函数 demo

来源:互联网 发布:男模项海 知乎 编辑:程序博客网 时间:2024/03/29 16:50

内容简介:在asp.net application中使用json rpc 调用方法,获取返回值

1、vs新建空asp.net application ,这里.net framework选择 4.0 + 

2、下载最新版的JSON-RPC.NET Asp.Net package

地址:

http://jsonrpc2.codeplex.com/releases/view/100345

这里下载的是JSON-RPC.NET Server 1.0.3

3、引入以下两个文件


4、同时要引入


否则项目调试运行会出错

下载地址 http://json.codeplex.com/

当前项目引入的是 net 4.0版

5、在web.config文件中添加相应的Http Handler

服务器为(ASP.NET development server) 或者IIS6 则添加下列内容到 <system.web>标签中

<httpHandlers>      <add type="AustinHarris.JsonRpc.Handlers.AspNet.JsonRpcHandler" verb="*" path="*.rpc"/>    </httpHandlers>

调试服务器为 IIS 7 或以上版本,则添加如下内容到 <system.webServer>标签中

 <handlers>      <add name="jsonrpc" type="AustinHarris.JsonRpc.Handlers.AspNet.JsonRpcHandler" verb="*" path="*.rpc"/>    </handlers>

6、下一步是新建一个测试类,并继承自 JsonRpcService类,并引入相应的命名空间 using AustinHarris.JsonRpc;

public class HelloWorldService: JsonRpcService{    }

7、在该类中添加一个属性为  [JsonRpcMethod]的测试方法 

 [JsonRpcMethod]        private string helloWorld(string message){            return "Hello World "+ message;        }

8、添加Global.asax文件,并在其中添加一个静态成员

public class Global : System.Web.HttpApplication {    private static HelloWorldService service = new HelloWorldService();}


9、现在可以在我们的页面中通过json-rpc调用方法了,添加一个defualt页面,并发送一个post请求给咱们的json-rpc处理,这里的url为你项目实际的url和端口号

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="defualt.aspx.cs" Inherits="TestJsonRpc.defualt" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title></title>   <script type="text/javascript">    window.onerror=function(errorMessage,errorUrl,errorNum)      {          alert(errorMessage+errorUrl+errorNum);      }       var xmlHttp;       function createXmlHttp()       {          if(window.ActiveXObject)           xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");          else           xmlHttp=new XMLHttpRequest();       }       function startRequest()       {        try        {           createXmlHttp();         var url = "http://localhost:2667/json.rpc";            xmlHttp.open("post",url,true);         xmlHttp.setRequestHeader("content-length", 62); //post提交设置项           xmlHttp.setRequestHeader("User-Agent", "Fiddler"); //post提交设置项           xmlHttp.setRequestHeader("content-type", "Application/Json-Rpc"); //post提交设置项           xmlHttp.onreadystatechange =onComplete;         //将名值对发送到服务器           xmlHttp.send('{"method": "helloWorld", "params": ["Hello World"], "id": 1  }');         }         catch(e)         {           alert(e.message);         }        }       function onComplete()       {          if(xmlHttp.readyState==4&&xmlHttp.status==200)           {             //显示结果             alert(xmlHttp.responseText);                   }       }  </script>  </head><body>    <form id="form1" runat="server">    <div>        <input type="button" onclick ="startRequest()" value="TestJsonRpc" />    </div>    </form></body></html>

10、点击TestJsonRpc按钮,返回 

{"jsonrpc":"2.0","Result":"Hello World Hello World","Id":"1"}

此时表明函数调用成功,并获取到了返回值 


文章内容来自http://jsonrpc2.codeplex.com/ 更多关于jsonrpc的内容参看这里


附上demo的C#代码下载地址 点击这里下载