14.01.2015 Views

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

Eric lippert - Amazon Web Services

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

{<br />

private TimeKeeprContext db = new TimeKeeprContext();<br />

// GET api/TimeCard<br />

public IEnumerable GetTimeCards()<br />

{<br />

return db.TimeCards.AsEnumerable();<br />

}<br />

// GET api/TimeCard/5<br />

public TimeCard GetTimeCard(int id)<br />

{<br />

TimeCard timecard = db.TimeCards.Find(id);<br />

if (timecard == null)<br />

{<br />

throw new HttpResponseException(Request.<br />

CreateResponse(HttpStatusCode.NotFound));<br />

}<br />

return timecard;<br />

}<br />

…<br />

}<br />

}<br />

Custom Media Type<br />

Formatters and<br />

Content Types<br />

Thus, we’ve seen how the out of the box media formatters work,<br />

let’s see what options we have for formatting data with a custom<br />

media formatter.<br />

Use Case<br />

The information we have can be easily saved as Calendar items.<br />

So it would be nice if our service could serve up VCalendar items<br />

too. VCalendar is a data format that most calendaring tools<br />

support. If our <strong>Web</strong> API could serve up VCalendar items for each<br />

TimeCard, then they could be easily integrated with calendaring<br />

tools like Outlook.<br />

With this use case in mind, let’s create a<br />

VCalendarMediaTypeFormatter.<br />

Our Custom Media Type<br />

Formatter<br />

Custom Media Type Formatters subclass the<br />

MediaTypeFormatter abstract class or one of its<br />

implementations. We will subclass the<br />

BufferedMediaTypeFormatter.<br />

public class VCalendarMediaTypeFormatter :<br />

BufferedMediaTypeFormatter<br />

{<br />

…<br />

}<br />

First thing we do in the constructor is to clear out all Supported<br />

media types and only keep “text/calendar”, which is the standard<br />

header type for requesting calendar items.<br />

public VCalendarMediaTypeFormatter()<br />

{<br />

SupportedMediaTypes.Clear();<br />

SupportedMediaTypes.Add(new MediaTypeHeaderValue(“text/<br />

calendar”));<br />

}<br />

Next we override the SetDefaultContentHeaders and set<br />

content-disposition as an attachment with a generic file name<br />

(myFile.ics).<br />

public override void SetDefaultContentHeaders(Type type,<br />

HttpContentHeaders headers, MediaTypeHeaderValue<br />

mediaType)<br />

{<br />

headers.Add(“content-disposition”, (new<br />

ContentDispositionHeaderValue(“attachment”) {<br />

FileName = “myfile.ics” }).ToString());<br />

base.SetDefaultContentHeaders(type, headers,<br />

mediaType);<br />

}<br />

Since our formatter will not be accepting ICS files in posts, we’ll<br />

override the CanReadType method and return false directly.<br />

public override bool CanReadType(Type type)<br />

{<br />

return false;<br />

}<br />

Our Formatter should only try to format requests of type<br />

TimeCard and not any other type. So we limit this in the<br />

CanWriteType method by checking if the incoming type is of type<br />

TimeCard only.<br />

DNcmagazine www.dotnetcurry.com | 69

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!