So in this post, I will demonstrate how to reproduce the solution for those of you who are using ASP.NET Web API instead of WCF webHttpBinding. The concepts are similar, which is still leveraging on REST's flexibility and also a custom formatter. For, ASP.NET Web API, the task requires much lesser code. Here's the code to the custom formatter.
public class TextFormatter : MediaTypeFormatter
{Take note that I used Entities in my systems and therefore, the code relies heavily on the overridden ToString method to function correctly. If you are dealing with string types only, feel free to change it to your needs. Also take note that like in the previous article, I have not included the code to serialize collections in the example. If you need such functionality, you will need to build your own logic to do it.
public override bool CanReadType(Type type)
{
return type == typeof(object);
}
public override bool CanWriteType(Type type)
{
return type == typeof(object);
}
public override Task WriteToStreamAsync(Type type, object value,
Stream writeStream, HttpContent content,
TransportContext transportContext)
{
// Note: This example does not support serializing collection
// of entities.
return Task.Factory.StartNew(() =>
{
using (StreamWriter writer = new StreamWriter(writeStream))
{
// Entities must override the ToString() method.
writer.Write(value.ToString());
writer.Flush();
}
});
}
}
Here's the method for the ExpenseController:
public Expense GetExpenseById(long id)
{
var bc = new ExpenseComponent();
return bc.GetExpenseById(id);
}
Lastly, you will need to register the custom formatter to the Application_Start event in Global.asax.
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
GlobalConfiguration.Configuration.Formatters.Clear();
// Register the TextFormatter.
GlobalConfiguration.Configuration.Formatters.Add(new TextFormatter());
Take note that all your REST services under this application will be affected by this formatter.
Now when we call the REST service i.e.
http://localhost:8080/api/expenses/111615
We will get the exact result:
ExpenseID=111615,CorrelationID=00000000-0000-0000-0000-000000000000,Description=Test,Employee=,AssignedTo=,Remarks=
So there you have it. Supporting non-standardized response formats seems to be a very real
No comments:
Post a Comment