1- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
22< html xmlns ="http://www.w3.org/1999/xhtml ">
33< head >
44 < title > Custom media types vs data formats</ title >
125125 color : # 444 ;
126126 font-size : 11px ;
127127 }
128-
128+
129129 code { background-color : ghostWhite !important ; border : 1px solid # DEDEDE !important ; color : # 444 !important ; font-size : 12px !important ; line-height : 1.5em !important ; margin : 1em 0px !important ; overflow : auto !important ; padding : 0.5em !important ; display : block; font-family : monospace; margin : 1em 0px ; white-space : pre; }</ style >
130130</ head >
131131< body >
146146 < a id ="github " href ="https://github.com/ServiceStack/ServiceStack.Examples "> < img src ="img/btn-github.png " alt ="ServiceStack.Examples project " /> </ a >
147147
148148 < div id ="body ">
149-
149+
150150 < h1 > Custom media types vs data formats - VCard</ h1 >
151151 < p >
152152 An often overlooked feature in ServiceStack is its ability to easily support custom media types and formats.
153+ </ p >
154+ < p >
153155 It's not given as much prominence as ServiceStack's other formats simply because supporting specific media types
154- require more development effort to implement (since it doesn't come for free out-of-the-box)
156+ require more development effort to implement (since it doesn't come for free out-of-the-box)
155157 and by nature are tightly coupled to your specific application dataset making it non-reusable to your other services.
156158 </ p >
157159
158160 < h2 > Specific Media Types vs Data Formats</ h2 >
159161 < p >
160162 Unlike the other built-in ServiceStack formats (JSON, XML, CSV, JSV, HTML, SOAP), specific media formats like the
161- < a href ="http://en.wikipedia.org/wiki/VCard "> VCard format</ a > cannot be used (nor makes sense to) with anything
162- but your contact data.
163+ < a href ="http://en.wikipedia.org/wiki/VCard "> VCard format</ a > cannot be used (nor makes sense to) with anything
164+ but your contact data. Although if your OS supports it, it enables a deeper integration into your applications than otherwise possible:
163165 </ p >
164166
167+ < a href ="customers/ALFKI?format=x-vcard "> < img src ="img/vcard-ALFKI.png " alt ="Maria Anders VCard " style ="margin:10px; " /> </ a >
168+
165169 < h3 > Supporting the VCard Format in ServiceStack</ h3 >
166170 < p >
167- Like the
168- < a href ="https://github.com/ServiceStack/ServiceStack/wiki/HTML5ReportFormat "> HTML</ a > and
169- < a href ="https://github.com/ServiceStack/ServiceStack/wiki/ServiceStack-CSV-Format "> CSV</ a >
170- formats before it, the best way to add an additional Media Type in ServiceStack is to encapsulate it in a single
171+ Like the
172+ < a href ="https://github.com/ServiceStack/ServiceStack/wiki/HTML5ReportFormat "> HTML</ a > and
173+ < a href ="https://github.com/ServiceStack/ServiceStack/wiki/ServiceStack-CSV-Format "> CSV</ a >
174+ formats before it, the best way to add an additional Media Type in ServiceStack is to encapsulate it in a single
171175 class keeping it loosely-coupled from the rest of your system thus making it an easy drop-in or removal whenever you need it.
172- The entire support for the format is contained in
176+ The entire support for the format is contained in
173177 < b > < a href ="https://github.com/ServiceStack/ServiceStack.Examples/blob/master/src/ServiceStack.Northwind/ServiceStack.Northwind.ServiceInterface/VCardFormat.cs "> VCardFormat.cs</ a > </ b >
174178 and is explained below:
175179 </ p >
176180
177181 < h4 > Registering a custom format in ServiceStack</ h4 >
178- < p >
179- In the < b > Register()</ b > method we simply tell ServiceStack that we have a new < b > ContentType</ b > available and
182+ < p >
183+ In the < b > Register()</ b > method we simply tell ServiceStack that we have a new < b > ContentType</ b > available and
180184 supply the Stream serialisers that it should use whenever processing that ContentType.
181185 </ p >
182186 < p >
183187 With just this 1 line of config:
184188 < b > < code > appHost.ContentTypeFilters.Register(VCardContentType, SerializeToStream, DeserializeFromStream);</ code > </ b > < br />
185- We now have your custom format registered into ServiceStack who will now use the supplied Content serializers
189+ We now have your custom format registered into ServiceStack who will now use the supplied Content serializers
186190 whenever this Content-Type is requested by the client in any of the following ways:
187191 </ p >
188192
@@ -193,13 +197,13 @@ <h4>Registering a custom format in ServiceStack</h4>
193197 </ ul >
194198
195199 < p >
196- Another benefit ServiceStack gives you is that this format is automatically provided on the auto-generated
200+ Another benefit ServiceStack gives you is that this format is automatically provided on the auto-generated
197201 < a href ="metadata "> /metadata</ a > page.< br />
198202 This is what the Customers X-VCARD metadata page looks like: < a href ="x-vcard/metadata?op=Customers "> /x-vcard/metadata?op=Customers</ a >
199203 </ p >
200-
204+
201205 < p >
202- The ResponseFilter is added to intercept the response and a Content-Disposition HTTP header added to signal to the
206+ The ResponseFilter is added to intercept the response and a Content-Disposition HTTP header added to signal to the
203207 browser that this resource is to be treated as an attachment using the prescribed filename.
204208 </ p >
205209
@@ -257,11 +261,11 @@ <h4>Registering a custom format in ServiceStack</h4>
257261 + new[] { customer.Address, customer.City, customer.PostalCode }.Join(";"));
258262 sw.WriteLine("END:VCARD");
259263}</ code >
260-
264+
261265 < p >
262- And that's the entire VCard format in a single loosely-coupled class.
266+ And that's the entire VCard format in a single loosely-coupled class.
263267 No other concepts / artefacts are required to make this work, you can just simply plug it
264- into ServiceStack the the following 1 Line of Code:
268+ into ServiceStack the the following 1 Line of Code:
265269 </ p >
266270
267271 < code > public override void Configure(Container container)
0 commit comments