Asp.net Web Api 数据绑定

1、假设有如下api,传入经纬度获取城市信息,api可以定义为api/geodata?latitude=47.678558&Longitude=-122.130989 下面我来尝试将经纬度信息作为一个参数进行提交。

api/geodata?location=47.678558,-122.130989

首先,我们肯定想到可以在api中获取location再去对location进行解析,这种方法不推荐,下面我们尝试另外一种方法直接变量接收。

[TypeConverter(typeof(GeoPointConverter))]//标记类型转换器
    public class GeoPoint
    {
        public double Latitude { get; set; }
        public double Longitude { get; set; }

        public static bool TryParse(string s, out GeoPoint result)
        {
            result = null;

            var parts = s.Split(‘,‘);
            if (parts.Length != 2)
            {
                return false;
            }

            double latitude, longitude;
            if (double.TryParse(parts[0], out latitude) &&
                double.TryParse(parts[1], out longitude))
            {
                result = new GeoPoint() { Longitude = longitude, Latitude = latitude };
                return true;
            }
            return false;
        }
    }

    //类型转换器
    class GeoPointConverter : TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {//检查是否能够转换
            if (sourceType == typeof(string))
            {
                return true;
            }
            return base.CanConvertFrom(context, sourceType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {//进行数据类型转换
            if (value is string)
            {
                GeoPoint point;
                if (GeoPoint.TryParse((string)value, out point))
                {
                    return point;
                }
            }
            return base.ConvertFrom(context, culture, value);
        }
    }

以上包括一个实体类和一个类型转换类,由类型转换类负责把string类型的数据转化成GeoPoint。

Action定义如下:

        [Route("geodata")]
        public IHttpActionResult GetGeoData(GeoPoint geoPoint)
        {
            return Ok(geoPoint);
        }
时间: 2024-08-25 20:52:45

Asp.net Web Api 数据绑定的相关文章

通过Knockout.js + ASP.NET Web API构建一个简单的CRUD应用

REFERENCE FROM : http://www.cnblogs.com/artech/archive/2012/07/04/Knockout-web-api.html 较之面向最终消费者的网站,企业级Web应用对用户体验的要求要低一些.不过客户对“用户体验”的要求是“与日俱增”的,很多被“惯坏了”的用户已经不能忍受Postback带来的页面刷新,所以Ajax在企业级Web应用中得到了广泛的应用.企业级Web应用的一个特点是以“数据处理”为主,所以“面向绑定”的Knockout.js 是一

ASP.Net Web API 的参数绑定[翻译]

原文地址:Parameter Binding in ASP.NET Web API 译文如下: 当Web API相应Controller的一个方法时,它必定存在一个设置参数的过程,叫作数据绑定.这篇文章描述了Web API如何绑定参数以及如何自定义绑定过程. 一般情况下,Web API绑定参数符合如下规则: 如果参数为简单类型,Web API 尝试从URI中获取.简单参数类型包含.Net源生类型(int,bool,double...),加上TimeSpan,DateTime,Guid,decim

【ASP.NET Web API教程】3.3 通过WPF应用程序调用Web API(C#)

参考页面: http://www.yuanjiaocheng.net/ASPNET-CORE/core-static-files.html http://www.yuanjiaocheng.net/ASPNET-CORE/setup-mvc.html http://www.yuanjiaocheng.net/ASPNET-CORE/mvc-design-pattern.html http://www.yuanjiaocheng.net/ASPNET-CORE/mvc-routing.html h

ASP.NET Web API Model-ParameterBinding

ASP.NET Web API Model-ParameterBinding 前言 通过上个篇幅的学习了解Model绑定的基础知识,然而在ASP.NET Web API中Model绑定功能模块并不是被直接调用的,而是要通过本篇要介绍的内容ParameterBinding的一系列对象对其进行封装调用,通过本篇的学习之后也会大概的清楚在Web API中绑定会有哪几种方式. Model-ParameterBinding(对象篇) 在ASP.NET Web API中ParameterBinding代表着

【ASP.NET Web API教程】2.3.5 用Knockout.js创建动态UI

注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. Part 5: Creating a Dynamic UI with Knockout.js 第5部分:用Knockout.js创建动态UI 本文引自:http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-entity-framework/using-web-api-with-entity-fra

ASP.NET Web API 2基于令牌的身份验证

基于令牌的认证 我们知道WEB网站的身份验证一般通过session或者cookie完成的,登录成功后客户端发送的任何请求都带上cookie,服务端根据客户端发送来的cookie来识别用户. WEB API使用这样的方法不是很适合,于是就有了基于令牌的认证,使用令牌认证有几个好处:可扩展性.松散耦合.移动终端调用比较简单等等,别人都用上了,你还有理由不用吗? 下面我们花个20分钟的时间来实现一个简单的WEB API token认证: Step 1: 新建一个空的WEB API项目,项目名称就设置为

Asp.Net Web API 2第四课——HttpClient消息处理器

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 Asp.Net Web API 导航   Asp.Net Web API第一课:入门http://www.cnblogs.com/aehyok/p/3432158.html Asp.Net Web API第二课:CRUD操作http://www

使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【开篇】【持续更新中。。。】

小分享:我有几张阿里云优惠券,用券购买或者升级阿里云相应产品最多可以优惠五折!领券地址:https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=ohmepe03 最近发现web api很火,园内也有各种大神已经在研究,本人在asp.net官网上看到一个系列教程,原文地址:http://bitoftech.net/2013/11/25/detailed-tutorial-building-asp-net-

[转] ASP.NET WEB API程序在VS启动或发布到IIS后启动后发生 - Could not load file or assembly 'System.Web.Http.WebHost’异常,无法正常访问

Just do Copy Local = true in the properties for the assembly(System.Web.Http.WebHost) and then do a redeploy, it should work fine. http://stackoverflow.com/questions/20323107/could-not-load-file-or-assembly-system-web-http-webhost-after-published-to-