【问题标题】:Asp.Net MVC Model Binding to a property type System.Uri got issues with MonoAsp.Net MVC 模型绑定到属性类型 System.Uri 遇到 Mono 问题
【发布时间】:2014-06-09 10:56:25
【问题描述】:

在 Visual Studio 上进行调试时,效果非常好。即使没有默认构造函数,也只有参数化的。

但是,虽然托管在 linux server 中的 nginx 实例上。它抛出System.UriFormatException: Absolute URI is too short

虽然我已经实现了将其绑定到字符串类型然后将其转换为 Uri 的解决方法。但它不是一种更清洁的方式。

有人对此行为有任何想法吗?

【问题讨论】:

    标签: asp.net-mvc nginx mono


    【解决方案1】:

    好的,我终于明白了。在内部,当在 DefaultModelBinder 中执行 GetPropertyValue 时,它使用 Object.Equals(obj, string.Empty) 将 Object 与 string.empty 进行比较。

    当 obj 是 System.Uri 类型时,这会引发异常,因为 Object.Equals 会调用 Uri.Equals,然后它会尝试将强制转换的 emtoy 字符串类型转换为 Uri 类型。

    查看https://github.com/mono/mono/blob/master/mcs/class/System.Web.Mvc3/Mvc/DefaultModelBinder.cs的sn-p

    if (bindingContext.ModelMetadata.ConvertEmptyStringToNull && Object.Equals(value, String.Empty)) 
    {
         return null;
    }
    

    解决方案: 为 Uri 类型创建一个自定义绑定器,并在 BindModel 集内

    bindingContext.ModelMetadata.ConvertEmptyStringToNull = false;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多