-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Remove properties from ResourceExecutingContext #4290
Comments
Taking this as I've already done it 👍 |
@rynowak I have a case where I need separate JsonSerializerSettings for a specific Controller, and found this answer on StackOverflow. How would I implement something like this without using For reference, the code: using System;
using System.Linq;
using Newtonsoft.Json.Serialization;
using Microsoft.AspNet.Mvc.Filters;
using Newtonsoft.Json;
using Microsoft.AspNet.Mvc.Formatters;
namespace Teedl.Web.Infrastructure
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class MobileControllerConfiguratorAttribute : Attribute, IResourceFilter
{
private readonly JsonSerializerSettings serializerSettings;
public MobileControllerConfiguratorAttribute()
{
serializerSettings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Objects,
TypeNameAssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple,
Binder = new TypeNameSerializationBinder("Teedl.Model.Mobile.{0}, Teedl.Model.ClientMobile")
};
}
public void OnResourceExecuted(ResourceExecutedContext context)
{
}
public void OnResourceExecuting(ResourceExecutingContext context)
{
var mobileInputFormatter = new JsonInputFormatter(serializerSettings);
var inputFormatter = context.InputFormatters.FirstOrDefault(frmtr => frmtr is JsonInputFormatter);
if (inputFormatter != null)
{
context.InputFormatters.Remove(inputFormatter);
}
context.InputFormatters.Add(mobileInputFormatter);
var mobileOutputFormatter = new JsonOutputFormatter(serializerSettings);
var outputFormatter = context.OutputFormatters.FirstOrDefault(frmtr => frmtr is JsonOutputFormatter);
if (outputFormatter != null)
{
context.OutputFormatters.Remove(outputFormatter);
}
context.OutputFormatters.Add(mobileOutputFormatter);
}
}
} |
@geirsagberg Did you find a solution to your question? I'm facing the same issue. |
@parleer I honestly can't remember I'm afraid; I am no longer on the project where I had the problem :\ |
@geirsagberg I figured it out. The new way to do this is to use a TypeFilter which participates in Dependency Injection and so you can get an instance of IOptions and then setup the InputFormatters/OutputFormatters on OnActionExecuting(). |
I think the above solution will change the formatters for all requests, so you could run into a concurrency issue. Here's a solution I found that should customize the formatters per request and avoid a concurrency issue: https://stackoverflow.com/a/52193035/10391 |
We want to remove the following from
ResourceExecutingContext
https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Abstractions/Filters/ResourceExecutingContext.cs
We built this with a particular scenario in mind that we'd probably do another way if we started fresh today (named formatters).
Right now this is acting as an impediment to some other design changes so we want to simplify it.
The text was updated successfully, but these errors were encountered: