- Notifications
You must be signed in to change notification settings - Fork 86
Getting Started (Asp.Net)
Austin Harris edited this page Jul 10, 2017
·
3 revisions
Using json-rpc.net within a asp.net application;
-- Please see this issue until the wiki is updated. https://github.com/Astn/JSON-RPC.NET/pull/83
- Create a new ASP.NET Web Application.
- Use NuGet to Download the most recent JSON-RPC.NET Asp.Net package.
- Edit the web.config file and add a http handler.
For Casini (ASP.NET development server) and IIS6 add the following to <system.web>
<httpHandlers><add type="AustinHarris.JsonRpc.Handlers.AspNet.JsonRpcHandler"verb="*"path="*.rpc"/></httpHandlers>For IIS 7+ add the following to <system.webServer>
<handlers><add name="jsonrpc" type="AustinHarris.JsonRpc.Handlers.AspNet.JsonRpcHandler" verb="*" path="*.rpc"/>
</handlers>Create a new class that inherits from JsonRpcService.
publicclassHelloWorldService:JsonRpcService{}Inside your new class create a new method, and attribute it with [JsonRpcMethod].
[JsonRpcMethod]privatestringhelloWorld(stringmessage){return"Hello World "+message;}In Global.asax.cs add a new static member of your new service class type.
publicclassGlobal:System.Web.HttpApplication{privateHelloWorldServiceservice;// important that you do this as part of the asp.net lifecycle instead of as static due to the way IIS works. overridevoidinit(){service=newHelloWorldService();}}Test it. POST a JSON-RPC request to /json.rpc.
POSThttp://localhost:49718/json.rpc HTTP/1.1
User-Agent: Fiddler
Content-Type: Application/Json-Rpc
Host: localhost:49718
Content-Length:62{"method":"helloWorld","params":["Hello World"],"id":1}And you should receive a response.
HTTP/1.1200 OK
Server:ASP.NET Development Server/10.0.0.0
Date:Mon,29 Aug 201105:21:22 GMT
X-AspNet-Version:4.0.30319
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length:58
Connection: Close
{"Result":"Hello World Hello World","Error":null,"Id":"1"}