-
Notifications
You must be signed in to change notification settings - Fork 18
Url Form Encoded Formatter Overview
The form url encoded formatter will serialize and deserialize form url encoded values. Parameter names are made up of the full path in the object graph delimited by a period. Items in a collection will be suffixed with pound sign and the numerical index of the item, starting with the second item.
The form url encoded formatter can be configured declaratively on the service with the WcfRestContrib.ServiceModel.Description.WebDispatchFormatterConfigurationAttribute and WebDispatchFormatterMimeTypeAttribute:
{{
[WebDispatchFormatterConfiguration(“application/x-www-form-urlencoded”)]
[WebDispatchFormatterMimeType(
typeof(WcfRestContrib.ServiceModel.Dispatcher.Formatters.FormUrlEncoded),
“application/x-www-form-urlencoded”)]
public class Books : IBooksService {…}
}}
Or in configuration:
{{
}}
One or more formatters can be defined.
Lets take a look at a few examples. The following data contract:
{{
[DataContract]
public class Book
{
[DataMember]
public string Title { get; set; }
}
Book book = new Book() {
Title = “Night Thoughts of a Classical Physicist”,
Author = “Russell McCormmach”,
Published = 1991 };
}}
Will serialize as follows:
{{
Book.Title=Night+Thoughts+of+a+Classical+Physicist&Book.Author=Russell+McCormmach&Book.Published=1991
}}
If we change the contract to include a list:
{{
[DataContract]
public class Book
{
[DataMember]
public string Title { get; set; }
}
[CollectionDataContract(ItemName=“Author”)]
public class Authors : List { }
Book book = new Book() {
Title = “Expert F#”,
Authors = new Authors()
{
{"Don Syme"},
{"Adam Granicz"},
{"Antonio Cisternino"}
},
Published = 2007 };
}}
It will serialize as follows (Carriage return added for readability):
{{
Book.Title=Expert+F#&Book.Authors.Author=Don+Syme&Book.Authors.Author#2=Adam+Granicz&
Book.Authors.Author#3=Antonio+Cisternino&Book.Published=2007
}}
Posting the above information in an html form as follows:
{{
Title:
Author 1:
Author 2:
Author 3:
Published (Year):
}}
If the authors list contained a more complex type:
{{
[DataContract]
public class Book
{
[DataMember]
public string Title { get; set; }
}
[DataContract]
public class Author
{
[DataMember]
public string Name { get; set; }
}
[CollectionDataContract(ItemName=“Author”)]
public class Authors : List { }
Book book = new Book() {
Title = “Expert F#”,
Authors = new Authors()
{
new Author() { Name = “Don Syme”, PenName = “Rocky” },
new Author() { Name = “Adam Granicz”, PenName = “Bullwinkle” },
new Author() { Name = “Antonio Cisternino”, PenName = “Mr. Peabody” }
},
Published = 2007 };
}}
It will serialize as follows (Carriage returns added for readability):
{{
Book.Title=Expert+F#&Book.Authors.Author.Name=Don+Syme&Book.Authors.Author.PenName=Rocky&
Book.Authors.Author#2.Name=Adam+Granicz&Book.Authors.Author#2.PenName=Bullwinkle&
Book.Authors.Author#3.Name=Antonio+Cisternino&Book.Authors.Author#3.PenName=Mr.+Peabody&Book.Published=2007
}}
NOTE: Internally the Url Form Encoded formatter converts items to xml and sorts them alphabetically, which is how the DataContractSerializer reads them by default. If the data contract is part of an inheritance hierarchy and/or data members are explicitly ordered, this formatter will not work properly (See more [url:here|http://msdn.microsoft.com/en-us/library/ms729813.aspx]).
NOTE: The WcfRestContrib.ServiceModel.Web.WebServiceHost allows you to specify configuration based behaviors if you do not want to specify this declaratively. See more about it [here| Declarative Binding & Behavior Overview].