#Web API-Custom MediaTypeFormatters

1.You can create custom MediaTypeFormatter by inheriting abstract   BufferedMediaTypeFormatter. 

2.To add it to your web api,make changes in HttpConfiuration in Startup.cs

<pre class="prettyprint prettyprinted"><span class="pln"> config</span><span class="pun">.</span><span class="typ">Formatters</span><span class="pun">.</span><span class="typ">Add</span><span class="pun">(</span><span class="kwd">new</span> CustomFormat <span class="pun">());</span> 
</pre>

3. BufferedMediaTypeWritters  has two methods WriteToStream(write in custom format(Serialize)) and ReadFromStream(read the custom format(deserialize)).


public class CustomFormat :BufferedMediaTypeFormatter
{
public CustomFormat()
{
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/csv"));
}
public override bool CanReadType(Type type)
{
throw new NotImplementedException();
}

public override bool CanWriteType(Type type)
{
throw new NotImplementedException();
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s