理解中WebAPI的属性和相关操作 FormBody和 FormUri等(WebAPI 二)

本文深入探讨了WebAPI中FormUri和FormBody两种数据传递方式的使用场景与区别,包括URL参数传递、POST数据传递及如何指定Action名称进行方法调用。同时,介绍了NonAction属性的使用场景和AcceptVerbs属性的请求类型限定。

1.FromUri使用

将数据通过url方式传递。我们需要在webapi方法标明,这个参数只接受url中参数的值,

 $("#Save").click(function () {
            $.ajax({
                url: 'http://localhost:21162/api/person/?str1=1&str2=3',
                type: 'Get',
                dataType: 'json',
                success: function (data, textStatus, xhr) {
                    console.log(data);
                },
                error: function (xhr, textStatus, errorThrown) {
                    console.log('Error in Operation');
                }
            });
        });

在参数比较少情况下、或者Url长度小于256的情况下。可以考虑使用FromUri, 部分游览器会现在url大小,在这种情况下我们就要使用FromBody

2.FromBody

将数据通过post data方式方式传递。我们需要在webapi方法标明接受data参数,传递json参数

     

        $("#Save").click(function () {
                $.ajax({
                    url: 'http://localhost:21162/api/person/',
                    type: 'Get',
                    dataType: 'json',
                    data: { "str1": "1", "str2": "2" },
                    success: function (data, textStatus, xhr) {
                        console.log(data);
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        console.log('Error in Operation');
                    }
                });
            });

 3.ActionName使用,通过制定Action来调用方法

        [ActionName("GetAllPerson")]
        public IEnumerable<Person> GetAllPerson([FromUri] string str1, [FromUri] string str2)
        {
            return new List<Person>() { 
                new Person() {ID="1", Name="李鸿章ALL",Age=15}, 
                new Person() {ID="2", Name="杨玉红ALL",Age=15} };
        }

 

      $("#Save").click(function () {
                $.ajax({
                    url: 'http://localhost:21162/api/person/GetAllPerson?str1=1&str2=3',
                    type: 'Get',
                    dataType: 'json',
                    //data: { "str1": "1", "str2": "2" },
                    success: function (data, textStatus, xhr) {
                        console.log(data);
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        console.log('Error in Operation');
                    }
                });
            });

 

4.NonAction  表示不被外部应用程序调用

5. [AcceptVerbs("GET","POST")]

        [ActionName("GetAllPerson")]  //指定ActionName
        [NonAction]  //不被外部应用程序调用
        [AcceptVerbs("GET","POST")] //表示既可以Get请求,又可以Post请求
        public IEnumerable<Person> GetAllPerson([FromUri] string str1, [FromUri] string str2)
        {
            return new List<Person>() { 
                new Person() {ID="1", Name="李鸿章ALL",Age=15}, 
                new Person() {ID="2", Name="杨玉红ALL",Age=15} };
        }

 

posted on 2015-08-12 15:36  RomanticCode 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/RomanticCode/p/4724557.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值