Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. return new HttpResponseMessage
  2. {
  3. Content = new ObjectContent<string>("Hello world", new JsonMediaTypeFormatter()),
  4. StatusCode = HttpStatusCode.OK
  5. };
  6.  
  7. public class VMRegistrant
  8. {
  9. public int MerchantId { get; set; }
  10. public string Email { get; set; }
  11. }
  12.  
  13. public HttpResponseMessage CreateRegistrant(VMRegistrant registrant)
  14. {
  15. // Save registrant in db...
  16. }
  17.  
  18. public IHttpActionResult Foo()
  19. {
  20. var bar = new Bar { Message = "Hello" };
  21. return Request.CreateResponse(HttpStatusCode.OK, bar, new MediaTypeHeaderValue("application/json"));
  22. }
  23.  
  24. public IHttpActionResult Foo()
  25. {
  26. var bar = new Bar { Message = "Hello" };
  27. return Content(HttpStatusCode.OK, bar, new JsonMediaTypeFormatter(), new MediaTypeHeaderValue("application/json"));
  28. }
  29.  
  30. public async Task<IHttpActionResult> FooAsync()
  31. {
  32. var json = await Request.Content.ReadAsStringAsync();
  33. var content = JsonConvert.DeserializeObject<VMRegistrant>(json);
  34. }
  35.  
  36. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)]
  37. public class ChangeFormatterAttribute : ActionFilterAttribute
  38. {
  39. private IEnumerable<MediaTypeFormatter> oldFormatters;
  40. private MediaTypeFormatter desiredFormatter;
  41.  
  42. public ChangeFormatterAttribute(Type formatterType)
  43. {
  44. this.desiredFormatter = Activator.CreateInstance(formatterType) as MediaTypeFormatter;
  45. }
  46.  
  47. public override void OnActionExecuting(HttpActionContext actionContext)
  48. {
  49. var formatters = actionContext.ControllerContext.Configuration.Formatters;
  50.  
  51. oldFormatters = formatters.ToList();
  52.  
  53. formatters.Clear();
  54. formatters.Add(desiredFormatter);
  55.  
  56. base.OnActionExecuting(actionContext);
  57. }
  58.  
  59. public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
  60. {
  61. var formatters = actionExecutedContext.ActionContext.ControllerContext.Configuration.Formatters;
  62.  
  63. formatters.Clear();
  64. formatters.AddRange(oldFormatters);
  65.  
  66. base.OnActionExecuted(actionExecutedContext);
  67. }
  68. }
  69.  
  70. [ChangeFormatterAttribute(typeof(JsonMediaTypeFormatter))]
  71. public class HomeController : ApiController
  72. {
  73. public string Get()
  74. {
  75. return "ok";
  76. }
  77. }
  78.  
  79. // ...
  80.  
  81. [ChangeFormatterAttribute(typeof(XmlMediaTypeFormatter))]
  82. public class ValuesController : ApiController
  83. {
  84. public string Get()
  85. {
  86. return "ok";
  87. }
  88. }
  89.  
  90. public class Dog
  91. {
  92. public string Name { get; set; }
  93. }
  94.  
  95. public class DogMediaTypeFormatter : JsonMediaTypeFormatter
  96. {
  97. public override bool CanReadType(Type type)
  98. {
  99. return type == typeof (Dog);
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement