473,320 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

processing REST requests in C#

mmc
Hi all,
I've been using C# for a long time, but am somewhat new to REST, and
have what I'm sure are real newbie questions:

I want to develop a RESTful Web Service API that will be able to
handle requests of the form

http://mydomain.com/widgets/france --(returns all widgets available
in france)
http://mydomain.com/widget/1234 -- (returns data about widget # 1234)
http://mydomain.com/widget/1234/costInDollars --(returns the price of
widget #1234 in $)
http://mydomain.com/widget?id=1235&n...&country=Spain
--(creates a new widget with an id of 1235, a name of NewWidget, a
costInDollars of $25.50, and an available-in-country of Spain.

My questions:
1) are these "RESTful calls"? (I'm pretty sure the first 3 are, but
is the last one a reasonable syntax for "add widget"?
2) On the server side, does the HTTP handler simply parse the URL to
retrieve the object type, ID, etc. (and retrieve the QueryString
params normally), or is there a streamlined way to break about the
URL?

Thanks in advance for any help!

Sep 4 '08 #1
4 3244
"mmc" <mi*************@gmail.comwrote in message
news:91**********************************@k36g2000 pri.googlegroups.com...
I've been using C# for a long time, but am somewhat new to REST, and
have what I'm sure are real newbie questions:

I want to develop a RESTful Web Service API that will be able to
handle requests of the form

http://mydomain.com/widgets/france --(returns all widgets available
in france)
http://mydomain.com/widget/1234 -- (returns data about widget # 1234)
http://mydomain.com/widget/1234/costInDollars --(returns the price of
widget #1234 in $)
http://mydomain.com/widget?id=1235&n...&country=Spain
--(creates a new widget with an id of 1235, a name of NewWidget, a
costInDollars of $25.50, and an available-in-country of Spain.

My questions:
1) are these "RESTful calls"? (I'm pretty sure the first 3 are, but
is the last one a reasonable syntax for "add widget"?
It's not. The URL you provided clearly indicates that it will be an HTTP GET
request. According to REST principles as applicable to HTTP, creating new
objects is represented by HTTP POST. So, HTTP GET for
http://mydomain.com/widgets/ returns the list of all widgets, and HTTP POST
to http://mydomain.com/widgets/ adds a new widget.

Also, you shouldn't vary singular/plural. So it's "widgets/1234", and not
"widget/1234" (this is to maintain the proper URL hierarchy - a specific
widget is a child of the entire collection of widgets).
2) On the server side, does the HTTP handler simply parse the URL to
retrieve the object type, ID, etc. (and retrieve the QueryString
params normally), or is there a streamlined way to break about the
URL?
To begin with, in .NET 3.5 SP1, there's ADO.NET Data Services, which
(almost) seamlessly provides RESTful services on top of anything that
implements IQueryable<Tand IUpdatable. If you're a conventional REST layer
on top of your data model, it may well be just what you need (and if your
data storage is MSSQL, then you can use Entity Framework to map it to EDM,
which Data Services can consume, and save even more time).

Otherwise, why not use ASP.NET? You'll have to ignore all the Web Forms
stuff, of course, but you'll get to use some handy classes such as
HttpRequest and its Form property, which will parse those name/value pairs
in GET/POST/PUT requests for you.
Sep 4 '08 #2
mmc
Thanks very much, Pavel. That's exactly the kind of feedback I was
hoping for.

Regarding the server-side stuff, I plan to use ASP.NET, and will
explore the .NET 3.5 ADO.NET Data Services stuff you describe.
Happily, we're also using SQL Server, so I'll also look into the
Entity Framework stuff.

I still don't think I'm 100% clear on how to decompose the
"mydomain.com/widgets/1234/costInDollars" type URL into its composite
parts (e.g the widget ID, etc.), but maybe there are HttpRequest
methods and/or other mechanisms to do that for me.

Thanks again!

Sep 4 '08 #3

"mmc" <mi*************@gmail.comwrote in message
news:09**********************************@25g2000p rz.googlegroups.com...
Thanks very much, Pavel. That's exactly the kind of feedback I was
hoping for.

Regarding the server-side stuff, I plan to use ASP.NET, and will
explore the .NET 3.5 ADO.NET Data Services stuff you describe.
Happily, we're also using SQL Server, so I'll also look into the
Entity Framework stuff.

I still don't think I'm 100% clear on how to decompose the
"mydomain.com/widgets/1234/costInDollars" type URL into its composite
parts (e.g the widget ID, etc.), but maybe there are HttpRequest
methods and/or other mechanisms to do that for me.
Yes, you can use the same class that Data Services uses - it's called
UriTemplate, introduced in 3.5 (plain one, pre-SP1). You can define a
template like this (MSDN example):

weather/{state}/{city}?forecast={day}

and then you use UriTemplate.Match to match this template against the URI,
and retrieve the matched values from the returned UriTemplateMatch object.

You can look at the complete example here:
http://msdn.microsoft.com/en-us/libr...ate.match.aspx

By the way, it seems that WCF in 3.5+ allows very seamless creation of
RESTful web services based on those URI templates. It would look like this:
[DataContract(Namespace="")]
public class Customer
{
[DataMember]
public string ID { get; set; }
[DataMember]
public string Name { get; set; }
} [ServiceContract]
public interface IService
{
[OperationContract]
[WebGet(UriTemplate="customers/{id}")]
Customer GetCustomer(string id);

[OperationContract]
[WebInvoke(UriTemplate="customers")]
Customer PostCustomer(Customer c);
}Here's more on this:
http://blogs.msdn.com/kaevans/archiv...using-wcf.aspx

Sep 4 '08 #4
mmc
Thanks again, Pavel. I'm not sure 3.5 is an option for me, but it
might be. If so, it sounds like UriTemplate is just the ticket. I'll
also check out the blog.

I appreciate the on-target and succinct help!

Sep 4 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jean-Louis Vill | last post by:
=============================================================================== I installed PHP from an RPM, but Apache isn't processing the PHP pages! What's going on here? Somebody can help me ?...
1
by: Dmitriy A | last post by:
Hi, I have an problem that I've been struggling with for a week now and simply can not resolve. I have an MFC C++ application that sends a request to a servlet which sends back data...
5
by: Oleg Berfirer | last post by:
I'm running a resource-intensive stored procedure, which reads a file with about 50,000 lines with a BULK INSERT into a temp table, then goes through it and inserts a record for each line into...
1
by: DBC User | last post by:
I have a process that I am writing, which receives the requests through filewatcher. Once I get the request, I need to start a thread asyn. I shouldn't start the thread again until the running...
6
by: Hitesh | last post by:
We all know that IIS and asp.net are suppose to be muti-threaded applications running on a pre-emptive multi-tasking model. But, what I have found is that under the default installation of...
9
by: jazzslider | last post by:
I have a headache. I've done a LOT of research lately into XForms, and I am thoroughly convinced that a good implementation of this technology would help me immensely in converting my...
5
by: B. Williams | last post by:
I need some assistance with random access file processing. I have a function that I would like to change from sequential file processing to random access. Thanks in advance. void...
6
by: surfivor | last post by:
I may be involved in a data migration project involving databases and creating XML feeds. Our site is PHP based, so I imagine the team might suggest PHP, but I had a look at the PHP documentation...
5
by: Marian Steinbach | last post by:
Howdy! I am just switching from PHP4 to PHP5. Since there are many different ways to create XML in PHP5, I'd like to know from the community which one would be suited best for a REST web service...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.