Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Remove properties from ResourceExecutingContext #4290

Closed
rynowak opened this issue Mar 15, 2016 · 6 comments
Closed

Remove properties from ResourceExecutingContext #4290

rynowak opened this issue Mar 15, 2016 · 6 comments
Assignees
Milestone

Comments

@rynowak
Copy link
Member

rynowak commented Mar 15, 2016

We want to remove the following from ResourceExecutingContext

  • InputFormatters
  • ModelBinders
  • ValueProviderFactories
  • ValidatorProviders

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.

@Eilon Eilon added this to the 1.0.0-rc2 milestone Mar 15, 2016
@rynowak rynowak assigned rynowak and unassigned javiercn Mar 16, 2016
@rynowak
Copy link
Member Author

rynowak commented Mar 16, 2016

Taking this as I've already done it 👍

@geirsagberg
Copy link

@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 InputFormatters/ OutputFormatters on RequestExecutingContext?

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

@parleer
Copy link

parleer commented Jun 7, 2018

@geirsagberg Did you find a solution to your question? I'm facing the same issue.

@geirsagberg
Copy link

@parleer I honestly can't remember I'm afraid; I am no longer on the project where I had the problem :\

@parleer
Copy link

parleer commented Jun 7, 2018

@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().

@strawd
Copy link

strawd commented Sep 5, 2018

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants