I haven't been able to find a good answer to this problem or whether it is even possible. I want to make it so that my filter will add an object as a parameter using the context.ActionArguments["personService"] = personService. I am able to make an argument using a string type, but when I try to use a custom object I keep getting a 415 error using postman.
I know that I can pass an object using the context.HttpContext.Items.Add("personService",personService), but it is not an elegant solution for what I am doing. I have a class which is as follows:
public class PersonService
{
private readonly string _name;
private readonly int _age;
private readonly string _location;
public Person(string name, int age, string location)
{
_name = name;
_age = age;
_location = location;
}
public string printPerson(){
return "name: {0} age: {1} location {2}", _name, _age, _location";
}
}
Then my filter looks like the following:
public class PersonFilterAttribute: Attribute, IAsyncActionFilter
{
public PersonFilterAttribute()
{
}
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
context.ActionArguments["personService"] = new PersonService("Thomas", 29,"Miami");
await next();
}
}
[HttpGet("personService")]
[ServiceFilter(typeof(PersonFilterAttribute))]
public async Task<string> passTheObject(Person person)
{
return person.printPerson();
}