You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core OData中如何将IPAddress序列化为字符串

解决方案

1. 为IPAddress实现类型转换器

OData依赖类型转换逻辑识别自定义类型与Edm原生类型的映射,先创建一个IPAddressTypeConverter实现字符串与IPAddress的双向转换:

using System.ComponentModel;
using System.Net;

public class IPAddressTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
    {
        if (value is string ipStr)
        {
            return IPAddress.Parse(ipStr);
        }
        return base.ConvertFrom(context, culture, value);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string) && value is IPAddress ip)
        {
            return ip.ToString();
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

然后在程序启动时注册该转换器:

TypeDescriptor.AddAttributes(typeof(IPAddress), new TypeConverterAttribute(typeof(IPAddressTypeConverter)));

2. 修改Edm模型构建逻辑

不再把IPAddress注册为复杂类型,而是显式将TestModel的Ipaddress属性映射为Edm字符串类型:

public class EdmModelBuilder
{
    public IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();

        // 无需注册IPAddress为ComplexType
        var testEntity = builder.EntitySet<TestModel>("TestModel").EntityType;
        // 指定属性映射为Edm.String类型
        testEntity.Property(p => p.Ipaddress).As<string>();
        testEntity.Property(p => p.Ipaddress).IsRequired();

        return builder.GetEdmModel();
    }
}

3. 配置OData服务

在启动类中完成OData服务配置,确保模型与转换器生效(以.NET 6+为例):

var builder = WebApplication.CreateBuilder(args);

// 注册IPAddress类型转换器
TypeDescriptor.AddAttributes(typeof(IPAddress), new TypeConverterAttribute(typeof(IPAddressTypeConverter)));

// 添加OData服务并挂载模型
builder.Services.AddControllers().AddOData(options =>
{
    options.Select().Filter().OrderBy().Expand().Count().SetMaxTop(100);
    options.AddRouteComponents("odata", new EdmModelBuilder().GetEdmModel());
});

var app = builder.Build();

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

效果验证

  • 序列化:控制器返回的TestModel中Ipaddress会以字符串形式输出(如"192.168.1.1"),而非复杂对象结构。
  • 元数据:访问/odata/$metadata时,TestModel的Ipaddress属性会显示为<Property Name="Ipaddress" Type="Edm.String" Nullable="false" />。
  • 反序列化:客户端传入字符串格式的IP地址,OData会自动转换为IPAddress对象绑定到模型。

内容的提问来源于stack exchange,提问作者Christian Baker

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 19:52:32