问题
I'm using web api. I've been a bit lazy and decided to return a value tuple from my controller.
[HttpGet]
[Route(AuthAPIRoutes.GET_MFA_DEVICES)]
public (string Type, string Value)[] GetMultiFactoryMethods()
{
return GlobalFactory<IPaystreamMFASecurityService>.Instance.GetMultiFactorMethods();
}
The JSON response doesn't seem to be using the appropriate naming is this being optimized away?
{
"item1": "Phone",
"item2": "1-512-555-0550"
}
NOTE: I'm aware I can explicitly make a model to avoid this problem. I would like to understand what is occurring and why aren't my value tuple names being respected in the response?
回答1:
What is occurring is that ValueTuple as a type (a group of generic types, actually) is actually very static, and has properties named things like Item1
, Item2
, etc.
The nice syntax you get from C# where you can declare a name and have it used elsewhere in your code is simply a feature of the C# language. The compiled code referencing those values by name ends up calling into those static properties (Item1
, e.g.). And in fact, you can still access those properties by their "Item" names in your own C# code.
From the compiled code's perspective, the only clue about the names of the fields on those tuples is an attribute that gets associated with the method, so unless ASP.NET passed contextual information about the action method into the serializer, there would be no way for the serializer to know what names those properties were supposed to have.
See this article for an in-depth look at what ValueTuples are doing under the hood.
来源:https://stackoverflow.com/questions/56637736/value-tuples-expose-wrong-parameter-name-from-webapi