本人对webapi尚没有深入研究,初次接触发现了在数据请求时的几点现象。

先切入代码

1.如果action中开头带有Get默认就是get方式,不带Get默认就是post方式

 public string GetUsers0(string id)
{
List<UserProfile> list = UserProfileBiz.GetBatchTest() as List<UserProfile>;
//返回json字符串
return JsonConvert.SerializeObject(list); } $(function () { $.ajax({
url: "http://localhost:6556/api/User/GetUsers0",
type: "get",
dataType: "json",
data: {"ID":""},
beforeSend: function (XMLHttpRequest) {
},
success: function (data, status) {
$(data).each(function () {
alert(this.UserName);
});
},
complete: function (data, status) {
},
error: function () {
}
}); });

get方式可以请求到值,ID得大小写问题可以忽略,但是如果改成post方式,当然就请求不到了

如果改成post方式请求

就必须

 [HttpPost]
public string GetUsers0([FromBody]string id)
{
List<UserProfile> list = UserProfileBiz.GetBatchTest() as List<UserProfile>;
//返回json字符串
return JsonConvert.SerializeObject(list); }

现在讲action改成任意不带Get开头的,然后用get方式请求发现请求不到,这是因为action默认已变成post请求的方式,改成post后,没有问题

public string MyUsers0([FromBody]string id)
{
List<UserProfile> list = UserProfileBiz.GetBatchTest() as List<UserProfile>;
//返回json字符串
return JsonConvert.SerializeObject(list); } $.ajax({
url: "http://localhost:6556/api/User/MyUsers0",
type: "post",
dataType: "json",
data: {"ID":""},
beforeSend: function (XMLHttpRequest) {
},
success: function (data, status) {
$(data).each(function () {
alert(this.UserName);
});
},
complete: function (data, status) {
},
error: function () {
}
});

但是依旧需要一个FromBody特性声明,针对非对象类型的传值方式

如果改成实体参数会是什么情况呢

 public string MyUsers0(UserProfile user)
{
List<UserProfile> list = UserProfileBiz.GetBatchTest() as List<UserProfile>;
//返回json字符串
return JsonConvert.SerializeObject(list); }

对于实体类型参数并不需要声明FromBody特性了。

至于为什么需要继续探究。

asp.net webapi初探(一)的更多相关文章

  1. Asp.Net WebApi核心对象解析(下篇)

    在接着写Asp.Net WebApi核心对象解析(下篇)之前,还是一如既往的扯扯淡,元旦刚过,整个人还是处于晕的状态,一大早就来处理系统BUG,简直是坑爹(好在没让我元旦赶过来该BUG),队友挖的坑, ...

  2. ASP.NET WebApi OWIN 实现 OAuth 2.0

    OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. OAuth 允许用户提供一个令牌, ...

  3. Asp.Net WebApi核心对象解析(上篇)

    生活需要自己慢慢去体验和思考,对于知识也是如此.匆匆忙忙的生活,让人不知道自己一天到晚都在干些什么,似乎每天都在忙,但又好似不知道自己到底在忙些什么.不过也无所谓,只要我们知道最后想要什么就行.不管怎 ...

  4. ASP.NET WebApi 文档Swagger深度优化

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明博客园蜗牛原文地址,cnblogs.com/tdws   写在前面 请原谅我这个标题党,写到了第100篇随笔,说是深度优化,其实也并没有什么深度 ...

  5. ASP.NET WebApi 文档Swagger中度优化

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文地址:www.cnblogs.com/tdws   写在前面 在后台接口开发中,接口文档是必不可少的.在复杂的业务当中和多人对接的情况下,简 ...

  6. Creating a Clean, Minimal-Footprint ASP.NET WebAPI Project with VS 2012 and ASP.NET MVC 4

    Creating a Clean, Minimal-Footprint ASP.NET WebAPI Project with VS 2012 and ASP.NET MVC 4 Building O ...

  7. ASP.NET WEBAPI 的身份验证和授权

    定义 身份验证(Authentication):确定用户是谁. 授权(Authorization):确定用户能做什么,不能做什么. 身份验证 WebApi 假定身份验证发生在宿主程序称中.对于 web ...

  8. Enable Cross-Origin Requests in Asp.Net WebApi 2[Reprint]

    Browser security prevents a web page from making AJAX requests to another domain. This restriction i ...

  9. 为Asp.net WebApi 添加跨域支持

    Nuget安装包:microsoft.aspnet.webapi.cors 原文地址:https://www.asp.net/web-api/overview/security/enabling-cr ...

随机推荐

  1. Leetcode: Count Numbers with Unique Digits

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  2. Lintcode: Segment Tree Query

    For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding ...

  3. HTML调用servlet(一)

    1.页面的数据表单 在使用Servlet处理用户请求之前,先准备一个页面,该页面用来提供数据表单.数据表单就是HTML中的<form>...</form>部分,当用户单击Sub ...

  4. Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作

    HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的 ...

  5. csuoj 1396: Erase Securely

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1396 1396: Erase Securely Time Limit: 1 Sec  Memory ...

  6. android xutils

    http://blog.csdn.net/rishengcsdn/article/details/47279851/

  7. 从C#的Singleton设计模式

    近来,我在学习如何在C#语言中使用设计模式中读到一些资料,其中有关Singleton设计模式引起了我的注意. 学过设计模式的开发者都知道Singleton模式.我想简要地解释一下这个设计模式是为那些尚 ...

  8. getResource().getPath()返回的路径空格变成了 %20

    this.getClass().getResource(“/”).getPath()使用者方法查看文件在服务器上的地址,但是地址中的空格会被转化为%20. 解决办法1: URI uri = new U ...

  9. ubuntu遇到的命令

    sudo passwd root这个命令是给root用户设定密码.su root切换到root用户.sudo cp 文件 /var/www移动文件到一个目录unzip xxx.zip解压zip文件mk ...

  10. Test Android with QTP

    by Yaron Assa I have recently come across a plug-in to QTP that enables to automate tests on Android ...