0

I would like to prepend all JSON responses with following string:

)]}',\n

What is the best way to achieve this with ASP.NET Core? I would like to have it enabled for all JSON responses automatically.

GSerg
  • 71,102
  • 17
  • 141
  • 299
stephan.peters
  • 845
  • 8
  • 29
  • https://stackoverflow.com/a/49189185/11683 – GSerg Nov 16 '18 at 12:30
  • Just curious: why ? SHouldn't the proper json syntax be produced by a library (typically json.net) ? Why manipulating json string ? – Steve B Nov 16 '18 at 13:13
  • 1
    https://angular.io/guide/security#xssi – stephan.peters Nov 16 '18 at 13:17
  • So you want to prevent [json hijacking](https://stackoverflow.com/q/2669690/11683). It may already [not be relevant](https://stackoverflow.com/q/16289894/11683). The idiomatic ASP.NET MVC way to protect from it was [JsonRequestBehavior.DenyGet](https://stackoverflow.com/q/8464677/11683), which [was removed](https://stackoverflow.com/questions/38578463/asp-net-core-the-name-jsonrequestbehavior-does-not-exist-in-the-current-cont#comment66001613_38578463) from MVC Core, apparently because it is not relevant anymore. – GSerg Nov 16 '18 at 21:55

1 Answers1

0

You could use a Middleware that checks the following condition (or whatever condition you need).

if(context.Response.ContentType == "application/json")

Then you just append prepend the context.Response.Body with your string.

Senne
  • 154
  • 9