DEV Community

Bipin Radhakrishnan
Bipin Radhakrishnan

Posted on • Originally published at blog.bipinr.com on

Alternate ways to intercept and manipulate response in Asp.Net core - Part 2

The Problem:

I have a web API with two endpoints, the endpoints serve data in XML format for two different vendors. The result of both the endpoints is the same object but there are some differences like namespaces. The problem is how to return the same object with only namespaces serialized differently? I didn't want to go with the obvious solution of serializing the object with the different namespaces and returning them.

Solution 1

Solution 2

Create Action Filters for the endpoints and manually serialize the output in the OnResultExecuting() method and write the serialized output to the response body.

Pros and Cons of the solution

Pros

  • The MVC pipeline remains unaltered, unlike Solution 1.
  • Because there is a separate action filter, there is a clear separation of concerns. Code is well separated, i.e business logic is taken care of by the API, and formatting the output is achieved by the formatters.

Cons

  • Since we start writing the response body, further manipulation of response is not possible. This is demonstrated in the mode using a middleware. <!--kg-card-end: markdown--> ### Implementation <!--kg-card-begin: markdown-->

Step 1:

I created two action filters one for the Washington weather station endpoint and Quebec endpoint.

Sol2-action-filters

Then I created a custom XML serializer to serialize the object into XML.

Sol2-xml-serializers

Step 2

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);
        }
    }

Enter fullscreen mode Exit fullscreen mode

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);
        }
    }

Enter fullscreen mode Exit fullscreen mode

I created a middleware called SimpleMiddleware in the solution to demonstrate one of the drawbacks of this solution and why I started searching for solution 3. The middleware simply sets the response status to 200.

Sol2-middleware

The middleware is commented in the Configure() method in the Startup.cs file.

you may uncomment and check out the error

 //app.UseSimpleMiddleware();

Enter fullscreen mode Exit fullscreen mode

error_middleware-1

<!--kg-card-end: markdown--><!--kg-card-begin: markdown-->

Output

Endpoint 1: Washington Weather Staion

<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>

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

Solution 3

Code

The sample code for solution is here

or https://github.com/BipinBlog/asp.net_response_manipulation

Solution : Serialize_in_action_filter.sln

Top comments (0)