Page view counter

REST.NET - Add REST RPC API to your ASP.NET Application

Last post 05-15-2008 6:49 AM by mit_ce. 4 replies.

Sort Posts:

  • REST.NET - Add REST RPC API to your ASP.NET Application

    11-29-2005, 3:45 PM
    • Loading...
    • jawngee
    • Joined on 11-29-2005, 8:31 PM
    • NYC
    • Posts 4
    • Points 20
    This is a library for developing a REST API in your web application on the ASP.NET framework. The source for the library can be found here HeavyMetal REST Library.

    What it does

    The library essentially will take any class marked with some specific attributes and expose it's methods via REST through your ASP.NET application. I'm not going to go into a description of what REST is, or it's merits over SOAP, I'll leave that for you to research. Usage of the library is simple. Download and unzip the above archive. Add a reference to the project to your ASP.NET web application. Modify your web.config file to include the following lines:

    <httphandlers>
        <add verb="*" path="rest/*/*.ashx" type="HeavyMetal.REST.RESTHandler,HeavyMetal.REST"></add>
    </httphandlers>


    To expose a class and it's methods, simply modify your class by applying RESTClass and RESTMethod attributes. You can assign friendly names to the class and methods which affect the URL that is called to invoke the method. For example:
    [HeavyMetal.REST.RESTClass]
    public class RESTSample
    {
    public RESTSample() {}

    [HeavyMetal.REST.RESTMethod]
    public string HelloWorld(string greetings)
    {
    return "Hello there "+greetings;
    }
    }
    In the above example, we've declared the class RESTSample as a REST class by applying the RESTClass attribute. You can now call this method:
    http://localhost/yoursite/rest/RESTSample/HelloWorld.ashx?greetings=Bob
    If we want to mask, or change, the URL to be more friendly, we could redeclare the class in this way:
    [HeavyMetal.REST.RESTClass("Test")]
    public class RESTSample
    {
    public RESTSample() {}

    [HeavyMetal.REST.RESTMethod("Hello")]
    public string HelloWorld(string greetings)
    {
    return "Hello there "+greetings;
    }
    }
    The URL for invoking the HelloWorld method has now changed to:
    http://localhost/yoursite/rest/Test/Hello.ashx?greetings=Bob
    If we were to go this URL, we'd get the following back:
    <response>Hello there Bob</response>
    
    How it Works
    An HttpHandler intercepts requests to the webserver that call anything in the path "~/rest/". The first time a REST method is invoked, the handler iterates through all of the loaded assemblies and all of the assemblies in the web app's bin folder. It finds any class marked with the RESTClass attribute and adds it to an internal hashtable. The HttpHandler then parses the path of the query, does a lookup on the hashtable to find a class matching the name and then looks to find a method matching the name. If it finds this, it will use reflection to invoke the method, passing in the parameters passed in the query string. Right now, parameters for methods can only be simple types; e.g. int, bool, string, datetime, etc. However, the method itself can return simple types and classes. If returning a class from a method, make sure it has a Serializable attribute applied. For example:
    [Serializable]
    public class Person
    {
    public string Name;
    public int Age;
    }

    [HeavyMetal.REST.RESTClass("Test")]
    public class RESTSample
    {
    public RESTSample() {}

    [HeavyMetal.REST.RESTMethod]
    public Person GetPerson(string who, int age)
    {
    Person result=new Person();
    result.Name=who;
    result.Age=age;
    return result;
    }
    }
    Now if we call this REST method:
    http://localhost/yoursite/rest/Test/GetPerson.ashx?who=Jon&age=32
    Will return:
    <Person>
    <Name>Jon</Name> <Age>32</Age>
    </Person>
    Notes
    This is a really rough version. If you have any ideas, bugs, patches, please email them to me here.

    Jon Gilkison
    interfacelab.com
  • Re: REST.NET - Add REST RPC API to your ASP.NET Application

    09-18-2007, 2:19 AM
    • Loading...
    • whiteshadow
    • Joined on 10-21-2006, 7:00 AM
    • Posts 1
    • Points 2

    Hi Jon,

     

    Nice post but I couldn't download the file.

    Could you give the updated link?

     

    Dax
     

  • Re: REST.NET - Add REST RPC API to your ASP.NET Application

    11-22-2007, 9:55 AM
    • Loading...
    • marcop69br
    • Joined on 11-22-2007, 2:51 PM
    • Posts 1
    • Points 2

    The link to download the file doesn't work.
    There is another link ?

    Tanks.
    Marco Polo

  • Re: REST.NET - Add REST RPC API to your ASP.NET Application

    01-28-2008, 11:26 PM
    • Loading...
    • MikeSchinkel
    • Joined on 10-10-2002, 11:49 PM
    • Atlanta, Georgia USA
    • Posts 131
    • Points 543

    Hi Jon:

    Why not announce on REST-discuss (and ask for feedback?)

    Question: Why no support for application/x-www-url-encoded for request mime-type and applications/json for response mime type?  Good REST services shouldn't dictate the client implementation.

    Also, are you aware that using verb (i.e. GetPerson) is not RESTful?  Your class really should have only four published methods: Get(), Post(), Put(), and Delete().  Having a "GetPerson" URL isn't RESTful because your architecture needs to support GET, POST, PUT, and DELETE for all URLs, where applicable:  This URL would be much better:

    GET /people/jon-32

    That would allow (with an appropriate content for updating the resource in the HTTP body):

    PUT /people/jon-32

    And also:

    DELETE /people/jon-32

    Finally, may I suggest reading RESTful Web Services?  After reading it you may want to rearchitect your library.

    HTH.

    P.S. There is another .NET REST library called Exyus; maybe you could collaborate?

    -Mike Schinkel
    http://www.mikeschinkel.com/blogs/
    http://www.welldesignedurls.org/
    Filed under:
  • Re: REST.NET - Add REST RPC API to your ASP.NET Application

    05-15-2008, 6:49 AM
    • Loading...
    • mit_ce
    • Joined on 05-15-2008, 12:46 AM
    • India
    • Posts 91
    • Points 574

    Hi Jon,

     Nice Post but not able to download the HeavyMetal REST Library. please do the needful.

     

    Thanks 

    Mit. 

     


     

    Thanks,
    Mitesh.

    Please Mark this as an Answer if it resolves your problem.
Page 1 of 1 (5 items)