Last active
December 28, 2015 01:19
-
-
Save BennyM/7420053 to your computer and use it in GitHub Desktop.
Get request and accept json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class iCalFormatter | |
: BufferedMediaTypeFormatter | |
{ | |
public iCalFormatter() | |
{ | |
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/iCal")); | |
} | |
public override bool CanReadType(Type type) | |
{ | |
return false; | |
} | |
public override bool CanWriteType(Type type) | |
{ | |
if (type == typeof(AppointmentModel)) | |
{ | |
return true; | |
} | |
else | |
{ | |
Type enumerableType = typeof(IEnumerable<AppointmentModel>); | |
return enumerableType.IsAssignableFrom(type); | |
} | |
} | |
public override void WriteToStream(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content) | |
{ | |
using (var writer = new StreamWriter(writeStream)) | |
{ | |
writer.WriteLine("BEGIN:VCALENDAR"); | |
writer.WriteLine("VERSION:2.0"); | |
var appointments = value as IEnumerable<AppointmentModel>; | |
if (appointments != null) | |
{ | |
foreach (var appointment in appointments) | |
{ | |
WriteAppointment(appointment, writer); | |
} | |
} | |
else | |
{ | |
var singleAppointment = value as AppointmentModel; | |
if (singleAppointment == null) | |
{ | |
throw new InvalidOperationException("Cannot serialize type"); | |
} | |
WriteAppointment(singleAppointment, writer); | |
} | |
writer.WriteLine("END:VCALENDAR"); | |
} | |
writeStream.Close(); | |
} | |
private void WriteAppointment(AppointmentModel appointment, StreamWriter writer) | |
{ | |
writer.WriteLine("BEGIN:VEVENT"); | |
writer.WriteLine("UID:" + appointment.Id); | |
writer.WriteLine("DTSTART:" + string.Format("{0:yyyyMMddTHHmmssZ}", appointment.From)); | |
writer.WriteLine("DTEND:" + string.Format("{0:yyyyMMddTHHmmssZ}", appointment.Until)); | |
writer.WriteLine("SUMMARY:" + appointment.Title); | |
writer.WriteLine("END:VEVENT"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[{"Id":1,"From":"2013-11-11T20:00:00","Until":"2013-11-11T21:00:00","Location":"Here","Title":"My title","Description":"My desc"}] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OutlookAgentMessageHandler | |
: DelegatingHandler | |
{ | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, | |
CancellationToken cancellationToken) | |
{ | |
if (request.Headers.UserAgent.Any(x => x.Comment != null && x.Comment.Contains("Microsoft Outlook"))) | |
{ | |
request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/iCal")); | |
} | |
return base.SendAsync(request, cancellationToken); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET http://localhost:58250/api/appointments HTTP/1.1 | |
User-Agent: Fiddler | |
Accept: application/json | |
Host: localhost:58250 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GET http://localhost:58250/api/appointments HTTP/1.1 | |
User-Agent: Fiddler | |
Accept: application/xml | |
Host: localhost:58250 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
config.Formatters.Add(new iCalFormatter()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class WebApiConfig | |
{ | |
public static void Register(HttpConfiguration config) | |
{ | |
config.Routes.MapHttpRoute( | |
name: "DefaultApi", | |
routeTemplate: "api/{controller}/{id}", | |
defaults: new { id = RouteParameter.Optional } | |
); | |
config.Formatters.Add(new iCalFormatter()); | |
config.MessageHandlers.Add(new OutlookAgentMessageHandler()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ArrayOfAppointmentModel xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MvcApplication12"><AppointmentModel><Description>My desc</Description><From>2013-11-11T20:00:00</From><Id>1</Id><Location>Here</Location><Title>My title</Title><Until>2013-11-11T21:00:00</Until></AppointmentModel></ArrayOfAppointmentModel> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment