Manipulate response in web api in .net Core MVC Pipeline - Solution 2
Using Action Filters for the endpoints and manually serializing the output

Search for a command to run...
Using Action Filters for the endpoints and manually serializing the output

No comments yet. Be the first to comment.
A 3 part series to talk about my experiment with trying to intercept and manipulate the response in .net core.
Create Action Filters for the two endpoints also created two output formatters
Create Action Filters for the two endpoints also created two output formatters

Using Action Filter to clear the output formatter

Welcome to the part 2 the problem.
A quick recap of the problem of what I'm trying to solve here, I have a web api that has two different endpoints that needs to serve the same data but with minor difference. Here I would be discussing about my second approach to solve this.
To demonstrate the solution I'm using a weather station api as my example, the api has two endpoints one returns the weather data for Quebec, Canada and other reports the weather data of Washington, U.S. Both the endpoint returns the data in xml format the schema is same, but the namespaces are different.
Create Action Filters for the endpoints and manually serialize the output in the OnResultExecuting() method and write the serialized output to the response body.
Create two action filters one for the Washington weather station endpoint and Quebec endpoint.

Created a custom XML serializer to serialize the object into XML.

Use the XML serializers in the corresponding action filters and serialize the object, here is a snippet from Washington action filter
// Action Filter
public class WashingtonOuputFilterAttribute : ActionFilterAttribute
{
public override async void OnResultExecuting(ResultExecutingContext context)
{
var response = context.HttpContext.Response;
if (response.StatusCode != 200)
{
base.OnResultExecuting(context);
}
var result = context.Result as ObjectResult;
var serializedResult = WashingtonOutputSerializer.Instance.Serialize(result.Value);
var resultBytes = Encoding.UTF8.GetBytes(serializedResult);
response.Headers.Add("Content-Type", "application/xml");
await response.Body.WriteAsync(resultBytes, 0, resultBytes.Length);
base.OnResultExecuting(context);
}
}
Here is the code of lazy-loaded singleton Washington XML Serializer
//Washington XML Serializer
public class WashingtonOutputSerializer
{
public static readonly Lazy<WashingtonOutputSerializer> lazyWashingtonSerializer =
new Lazy<WashingtonOutputSerializer>(() => new WashingtonOutputSerializer());
public static WashingtonOutputSerializer Instance
{
get { return lazyWashingtonSerializer.Value; }
}
private WashingtonOutputSerializer() { }
public string Serialize(object outputObject)
{
var namespaces = new XmlSerializerNamespaces();
namespaces.Add("WS", "http://www.wausweatherStation.com/xml/namespaces/type");
namespaces.Add("Metric", "http://www.wausweatherstation.com/xml/namespaces/siunit/temperature");
return XMLSerializer.Instance.Serialize(outputObject, namespaces);
}
}
Output
Endpoint 1: Washington Weather Station
<WeatherForecast xmlns:WS="http://www.wausweatherStation.com/xml/namespaces/type"
xmlns:Metric="http://www.wausweatherstation.com/xml/namespaces/siunit/temperature">
<Date>2020-06-02T13:50:52.5094204-04:00</Date>
<Temperature>
<Celsius>8</Celsius>
<Farenheit>46</Farenheit>
</Temperature>
<Summary>Mild</Summary>
</WeatherForecast>
Endpoint 2: Quebec Canada Weather Station
<WeatherForecast xmlns:WS="http://www.qubeccanadaweatherstation.com/xml/namespaces/type"
xmlns:Metric="http://www.qubeccanadaweatherstation.com/xml/namespaces/siunit/temperature">
<Date>2020-06-02T13:56:09.5506734-04:00</Date>
<Temperature>
<Celsius>34</Celsius>
<Farenheit>93</Farenheit>
</Temperature>
<Summary>Cool</Summary>
</WeatherForecast>
Pros
Start by creating a middleware called SimpleMiddleware in the solution to
demonstrate one of the drawbacks of this solution. The middleware simply sets the response status to 200.

The middleware is commented in the Configure() method in the Startup.cs file.
//app.UseSimpleMiddleware();

The sample code is here
Solution file: Serialize_in_action_filter.sln