【问题标题】:Web API ModelBinding From URI来自 URI 的 Web API 模型绑定
【发布时间】:2014-09-10 18:17:21
【问题描述】:

所以我有一个为DateTime 类型实现的自定义模型绑定器,我像下面这样注册它:

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    GlobalConfiguration.Configuration.BindParameter(typeof(DateTime), new CurrentCultureDateTimeAPI());
}

然后我设置了 2 个示例操作来查看我的自定义模型绑定是否发生:

    [HttpGet]
    public void BindDateTime([FromUri]DateTime datetime)
    {
        //http://localhost:26171/web/api/BindDateTime?datetime=09/12/2014
    }


    [HttpGet]
    public void BindModel([FromUri]User user)
    {
        //http://localhost:26171/web/api/BindModel?Name=ibrahim&JoinDate=09/12/2014
    }

当我从提到的 URL 运行和调用这两个操作时,userJoinDate 属性使用我配置的自定义绑定器成功绑定,但 BindDateTimedatetime 参数没有使用自定义绑定器绑定.

我已经在配置中指定所有DateTime 都应该使用我的自定义活页夹,那么为什么会冷漠呢?非常感谢您的建议。

CurrentCultureDateTimeAPI.cs:

public class CurrentCultureDateTimeAPI: IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        var date = value.ConvertTo(typeof(DateTime), CultureInfo.CurrentCulture);
        bindingContext.Model = date;
        return true;
    }
}

注意:如果我使用[FromUri(Binder=typeof(CurrentCultureDateTimeAPI))]DateTime datetime,那么它会按预期工作,但又是为什么?

【问题讨论】:

  • 这可能是因为您设置了 [FromUri] 属性 - Web API 使用格式化程序而不是模型绑定,因此不使用您的自定义模型绑定器。尝试删除 BindDateTime 方法中的 [FromUri] 属性。
  • @IlyaLuzyanin 不行。它不起作用。
  • 你说得对,[FromUri] 与此无关。我试图重现您的场景 - 一切正常,我的自定义模型绑定器在两种方法中都被调用。能否提供 CurrentCultureDateTimeAPI 代码?
  • @IlyaLuzyanin 当然。现在在帖子里。如果您能提供我的实现的复制代码以供参考,我将不胜感激。
  • 好吧,我也很惊讶,你自己试试吧 :) 我试图在 web api 2 源(github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/…github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/…)中找到原因,但没有运气。跨度>

标签: c# asp.net-web-api asp.net-web-api2


【解决方案1】:

也很令人惊讶:)

我最初的怀疑是这一行:

 GlobalConfiguration.Configuration.BindParameter(typeof(DateTime), new CurrentCultureDateTimeAPI());

MSDNGlobalConfiguration => GlobalConfiguration provides a global System.Web.HTTP.HttpConfiguration for ASP.NET application

但由于奇怪的原因,这似乎不适用于这种特殊情况。

所以,

只需在静态类WebApiConfig中添加这一行

 config.BindParameter(typeof(DateTime), new CurrentCultureDateTimeAPI());

这样您的 WebAPIConfig 文件看起来像:

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "web/{controller}/{action}/{datetime}",
                defaults: new { controller = "API", datetime = RouteParameter.Optional }
            );

            config.BindParameter(typeof(DateTime), new CurrentCultureDateTimeAPI());
        }

一切正常,因为这个方法是由WebAPI framework 直接调用的,所以你的CurrentCultureDateTimeAPI 肯定会被注册。

用您的解决方案对此进行了检查,效果很好。

注意:(来自cmets)你仍然可以支持Attribute Routing,你不需要注释掉config.MapHttpAttributeRoutes()这一行。

不过,如果有人能说出为什么 GlobalConfiguration 不起作用,那就太好了

【讨论】:

    【解决方案2】:

    您似乎想将一些数据发布到服务器。尝试使用 FromData 并发布 JSON。 FromUri 通常用于获取一些数据。使用 WebAPI 的约定并让它为您工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多