Cor*_*ith 10 c# asp.net-web-api
我IValueProvider为我的Web API项目创建了一些实现,我对该ContainsPrefix方法在接口上的用途感到困惑.
该ContainsPrefix方法有以下摘要评论:
确定集合是否包含指定的前缀.
然而,该方法的摘要是抽象的,并不解释prefix将向该方法提供什么或该方法所服务的功能.难道prefix会是动作参数的名字吗?动作名称?控制器名称?其中任何一个的前三个字母?此方法是否存在以自动确定IValueProvider应为哪个操作参数提供值?
我从来没有看到该ContainsPrefix方法由Web API框架被调用我的IValueProvider实现,虽然我确实看到网页API的参考CollectionModelBinder,MutableObjectModelBinder,SimpleModelBinderProvider,和CompositeValueProvider.例如,以下实现在我的测试中没有引起任何问题:
MyValueProvider:
public class MyValueProvider : IValueProvider
{
public bool ContainsPrefix(string prefix)
{
throw new NotYetImplementedException();
}
public ValueProviderResult GetValue(string key)
{
return new ValueProviderResult("hello", "hello", CultureInfo.InvariantCulture);
}
}
Run Code Online (Sandbox Code Playgroud)
的TestController:
public class TestController : ApiController
{
public string Get([ModelBinder]string input)
{
return input;
}
}
Run Code Online (Sandbox Code Playgroud)
GET对我的请求TestController将返回你好,所以我知道GetValue正在调用MyValueProvider,但没有抛出任何异常所以我知道ContainsPrefix没有被调用.
ContainsPrefix被Web API框架调用?prefix这个方法会提供什么?奇怪的是,您能够创建价值提供者而又不知道这种(非常重要的)方法的作用。首先,价值提供者做什么?简而言之,它们为您在操作中指定的参数提供值。例如:
public ActionResult Index(string test) {
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们使用名为“ test”的参数进行操作。在哪里获得价值?来自价值提供者。有几个内置提供程序,例如查询字符串或表单数据提供程序。这些提供程序将被一个接一个地调用,直到某些提供程序将能够提供值为止。例如,如果存在查询字符串参数“ test”-查询字符串值提供程序将注意到并返回一个值,因此将不会调用其他提供程序。接下来,如果发布数据包含参数“ test”-将使用它,依此类推。
因此,此参数名称(在这种情况下为“ test”)ContainsPrefix就是所谓的名称。以查询字符串值提供程序为例。如果查询字符串不包含“ test”,则此提供程序的ContainsPrefix将返回“ false”,并且将调用下一个值提供程序。如果返回true- GetValue应该返回值,并且不会调用任何其他提供程序。
如果您想为say cookie提供参数值,则在您的ContainsPrefix方法中,您将检查是否存在具有给定名称的cookie。请注意,只有在所有默认值提供程序都无法提供值的情况下,才会调用该方法。
因此,TLDR:前缀表示要为其提供值的参数名称。
| 归档时间: |
|
| 查看次数: |
1116 次 |
| 最近记录: |