RestLess - A Simple REST Framework Part 2

Posted on Wednesday, April 23, 2008

Some cool changes have come along for RestLess. Instead of blabbering on, I'll dive right into the changes.

Small naming changes

Based on Dave Donaldson's recommendation, IRESTReadable, RESTProperty, etc have their case changed. They are now IRestReadable, RestProperty, etc. Just do a rename all on REST to Rest if you already have implemented the framework.

No More Default.aspx, pretty urls!

A problem with the first version, was default.aspx was specified in the Url. This is ugly and not very RESTful. There is a new key you can add to web.config appSettings:

<add key="RestLess::GeneratePlaceholderDirs" value="true" />

When this value is set to true, RestLess will generate placeholder folders with an empty default.aspx document. This way you can specify the url as http://mysite.com/api/products/ (notice no default.aspx). Your web application directory will look something like below:

IRestWritable

IRestReadable, in the first version, allowed you to send an xml document down to a user submitting a GET request at the resource url. IRestWritable exposes one method void GetRestPostedObject(object obj). When a POST is submitted to the url/resource, this method will be called. The best thing about GetPostedObject(object obj) is that it will populate the object from querystring for you based on your RestProperty's. If the attribute is specified as IOType.Input or IOType.InputOutput, RestLess will look in the url for you to see if these values were passed by the user, then send you a populated object.

For example (copied the example object from the first version for clarity, new additions in orange)

    // decorate the class as a RestResource, the first parameter is the url of the resource
    // appended to the root url (defined in web.config). In this case /api/products/
    // the second parameter specifies if this resource is discoverable (more on this later)
   
[RestResource("products", true)]
    public class Product : IRestReadable, IRestWritable
    {
        private string _name;
        private string _description;
        private double _amount;

        // render this property as xmlelement "productname"
        // IOType.InputOutput specifies that this property will be rendered
        // to the outputted xml, and can also be inputted via querystring
       
[RestProperty("productname", IOType.InputOutput)]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        // render this property as xmlelement "productdescription"
        // IOType.Output specifies this property is rendered to the user,
        // but it is not allowed in querystring for input
       
[RestProperty("productdescription", IOType.Output)]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        // render this property as xmlelement "amount" (output only again)
       
[RestProperty("amount", IOType.InputOutput)
          public object GetRESTObject()
          {

              // this is where you could go access your data access layer or whatever
              //method you choose.
              // this is also where you could request an API key via querystring for security etc
              // since productname is specified inputoutput, you should check for
              // Request.QueryString["productname"] to get any data the user sends

              // for the purposes of this demo, lets just make an object and pass it back
              Product p = new Product();
              p.Name = "Super Fly Ink Pen";
              p.Description = "This is the most super fly ink pen ever";
              p.Amount = 10.00;

              return p;
          }

          #endregion

          #region IRestWritable Members

          public void GetRestPostedObject(object obj)
          {
              Product product = obj as Product;
  
              // product will be populated here based on anything provided in
QueryString.
              // you do not need to retrieve it manually. for example, if the POSTed url is
              // http://mysite.com/api/products/?productname=InkPen&amount=2.50

              product.Name // will equal InkPen
              product.Amount // will equal 2.50
          }
      
          #endregion

    }

Right now, IRestWritable only supports getting values from querystring. That will change later to support xml documents being posted, etc.

Grab the new dll here. (as of now, the plan is to open-source the framework when it's a little further along)

Enjoy!

Jayme

kick it on DotNetKicks.com

Comments

  • Scott Watermasysk on 4.24.2008 at 11:54 AM
  • What if IRestWritable as an empty interface? If it exists, your base page/httpModule would attempt to set the properties it discovers (via reflection).

    In addition, updates should leverage a post and not the querystring.

  • Jayme on 4.24.2008 at 1:39 PM
  • It does set the properties via reflection and pass the object to GetRestPostedObject(). If the interface was empty, how would you know the user posted anything at all?

    And yes - I agree on the post part. I would like to make it querystring, xml, json, etc all optional. Whatever the developer chooses.

  • Mycall on 5.01.2008 at 8:15 PM
  • Great library, keep up the good work.

    Keep in mind, RESTful interfaces do not have ? or & in the URL as those represent state and not a single resource.

    mysite.com/.../products

    should be

    mysite.com/.../2.50

    check out these articles as they might give you some good ideas:

    www.xml.com/.../rest.html http://www.w3.org/2001/tag/doc/hash-in-url zwah.com/.../rest-reduced-to

    Cheers!

  • Martin on 8.19.2008 at 5:26 PM
  • Hi Jayme

    I am interested in what you have done here and am wondering if you have made any progress of late.

    I see the artical is 23 April and we are now in August.

    Would like to know if this is evolving or RESTED(pardon the pun.)

    Thanks

    Martin

  • ricky on 5.07.2009 at 1:45 AM
  • thanks for sharing..

  • robert on 6.15.2009 at 9:17 PM
  • great example script, thanks..

  • bodybuilding forum on 6.16.2009 at 3:18 PM
  • Great, thank you for sharing it.

  • Stop Dreaming Start Action on 6.21.2009 at 10:58 PM
  • This tutorial help us for fix our problem.

  • Rusli Zainal Sang Visioner on 7.08.2009 at 1:08 PM
  • Thank you very much for the information I really appreciate it!! a found this nice site.

  • Stop Dreaming Start Action on 7.08.2009 at 1:10 PM
  • hello, this is my first time i visit here. I found so many interesting in your blog especially its discussion. keep up the good work.

    Regard,

    Mr.Joko Tingkir, ST

  • free online slot machine game downloads on 7.25.2009 at 5:37 AM
  • this is great information that i know a lot of people are interested in, thanks for sharing the code

  • alanda club marbella on 8.01.2009 at 8:19 AM
  • That's pretty interesting, thanks for the head ups my mate.

    regards,

  • Shelly on 8.06.2009 at 2:50 AM
  • thank you for sharing.

  • fatburning furnace on 8.12.2009 at 3:46 AM
  • Thanks a lot.

  • buy vimax store on 8.12.2009 at 3:47 AM
  • On the other hand, I like it a lot

  • rusli zainal sang visioner on 8.25.2009 at 1:32 AM
  • Thank you very much for the information I really appreciate it!!

  • Thanks on 8.31.2009 at 6:25 AM
  • Thanks for this all information...

    <a title="Lida" href="http://www.lida.name.tr">Lida</a>

    <a title="Lida" href="http://www.lidaturkiye.net">Lida</a>

    <a title="Lida" href="http://www.lidaturkiye.com">Lida</a>

    <a title="Lida" href="http://www.lidakapsul.org">Lida</a>

  • stop dreaming start action on 9.03.2009 at 10:01 AM
  • thank you for yor tutorial

  • rusli zainal sang visioner on 9.03.2009 at 10:03 AM
  • nice article, i like it. thank you

  • Buy Finasteride on 9.06.2009 at 12:09 PM
  • Thanks.

  • العاب بنات on 9.08.2009 at 1:07 AM
  • nice article, i like it. thank you

  • Dominating Mafia Wars on 9.09.2009 at 3:52 PM
  • Thank you for updating the cool changes.

  • Free online games on 9.17.2009 at 7:51 PM
  • Very interesting post..This is great information that i know a lot of people are interested in, thanks for sharing the code

  • Kenali dan Kunjungi Objek Wisata di Pandeglang on 9.19.2009 at 3:07 PM
  • Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

  • Jim @ 401k Rollover on 9.20.2009 at 11:51 AM
  • If I'm not mistaken, REST is used to create web service. It's nice to learn about it because I only know about SOAP.

  • mengembalikan jati diri bangsa on 9.23.2009 at 9:00 PM
  • nice share i like this one

  • kenali dan kunjungi objek wisata di pandeglang on 9.23.2009 at 9:01 PM
  • i love it

  • mengembalikan jati diri bangsa on 9.23.2009 at 9:03 PM
  • hemmm great post

  • whiten teeth on 9.27.2009 at 4:54 AM
  • Very interesting post..Iam very much enjoyed by reading your site..this article has provided a useful info..Thanks for the info given..

  • Lida on 9.28.2009 at 1:48 AM
  • thanks for this great post

  • moratmarit on 10.02.2009 at 8:48 AM
  • thanks for this usefull informations..

    now i find what i want to know..

    thanks..

  • cah bagoes on 10.02.2009 at 8:48 AM
  • thanks for this great post. this useful

  • oes tsetnoc on 10.02.2009 at 8:51 AM
  • I can here the new knowledge.

    Thanks for the great reference post.

  • luxury watches online on 10.02.2009 at 3:02 PM
  • This tutorial helpus us fixing our problem.

    Thanks a lot

  • anti aging products on 10.05.2009 at 9:17 AM
  • I am very glad to see such information which I was searching for a long time.This made very glad..This site has given us an useful information..

  • best teeth whitening products on 10.05.2009 at 9:22 AM
  • Very interesting site..Useful information shared..Iam very happy to read this article..thanks for giving us nice info..Keep up the work..

  • green tea for weight loss on 10.05.2009 at 9:31 AM
  • Useful information shared..Iam very happy to read this article..thanks for giving us nice info.Fantastic walk-through. I appreciate this post.

  • wine gifts on 10.07.2009 at 12:16 AM
  • Fantastic walk-through. I appreciate this post.There is obviously a lot to know about this. I think you made some good points in Feature also

  • kenali dan kunjungi objek wisata di pandeglang on 10.07.2009 at 8:49 PM
  • I was searching for a long time.This made very glad..This site has given us an useful information..

  • free online games on 10.08.2009 at 2:30 PM
  • I am very glad to see such information which I was searching for a long time.This made very glad..This site has given us an useful information..

  • stop excessive sweating on 10.11.2009 at 12:35 AM
  • Thanks Jayme for sharing the updates. Greatly appreciated.

  • Peluang Bisnis Sampingan on 2.06.2010 at 1:43 AM
  • What's the difference between REST and SOAP?

  • gioco ai casinò in rete on 2.18.2010 at 6:37 AM
  • I see the relationship between Web Site Projects (WSP) and Web Application Projects (WAP) as similar to that between Cassini and IIS (http://aspnetresources.com/blog/cassini_vs_iis.aspx) i.e. WSP are unprofessional. Even if a project is simple when created, building on a solid framework allows for easier expansion or future migration.

  • associate degree Nutrition on 2.25.2010 at 5:36 AM
  • I think all of us here were in need of something like this , thanks for providing writing this post.

  • Nutrition Diploma on 2.25.2010 at 5:37 AM
  • Good job, it's difficult to put together articles of this quality, thanks!

  • nutrition master degree on 2.25.2010 at 5:38 AM
  • This is actually really interesting regarding your fact article here, This article is very informative.

  • associate degree health science on 2.25.2010 at 5:38 AM
  • I think I found the right topic for my essay. The life and her passion is interesting and we will learn from that. I love music and someday I want my child to be a good instrumentalist.

  • doctorate degree nursing on 2.25.2010 at 5:39 AM
  • wow 20 years of practice? I haven't heard about Anni Rossi before but I will surely find out.

Leave a Comment


Search this site

Last 3 Tweets

  • The Courier looks insanely cool... i am very impressed.
  • @hkarthik or a badass PC desktop. they still have a good purpose :)
  • @JustinEtheredge woah. that sounds like a real gem there... lol