Modify every response based on a specific rule in .Net Core 3.1

Viewed 56

I'd like to target and modify specific fields on each http response.

I have a .net core 3.1 app on which I'd like to do the following:

whenever the response object has a string field in it, I'd like the response to be modified, example:

public class Info{
   public int Id { get; set; }
   public string InfoName { get; set; }
}

[HttpGet("GetInfoList")]
public async Task<List<Info>> GetInfoList()
{
   List<Info> infoList = new List<Info>();
   infoList.Add(new Info(){
      Id = 1;
      InfoName = "test name";
   })       

   return infoList;//or getting the info list from db context...   
}

[HttpGet("GetInfo/{id}")]
public async Task<Info> GetInfo(int id)
{
   return db.Info.Find(id);//from db context
}

So whenever the GetInfoList or GetInfo/1 is called, I'd like the response text on InfoName to be changed, let's say that currently the text is test name and I'd like it to be test name #. And in case I have another field of type string, in the same object, the rule should be applied there as well.

This is just an example, but in my app I have a lot of different objects that I'd like to target globally and change their values.

So, the best solution is to have a filter decorator so that I can target specific places by putting (in this case) [CustomStringRule] on each controller/action.. I've started reading the Filters in ASP.NET Core docs, but I guess this requires advanced knowledge.

0 Answers
Related