ASP.Net Web-api 不可多得的零基础教程7
2018-04-11 22:21
435 查看
Put方法主要用于修改信息。
public HttpResponseMessage Put(int id, Employee employee) { int index = list.ToList().FindIndex(e => e.Id == id); if (index >= 0) { list[index] = employee; // overwrite the existing resource return Request.CreateResponse(HttpStatusCode.NoContent); } else { list.Add(employee); var response = Request.CreateResponse<Employee>(HttpStatusCode.Created, employee); string uri = Url.Link("DefaultApi", new { id = employee.Id }); response.Headers.Location = new Uri(uri); return response; } }本篇的else模块类似于Post方法中的内容,这是因为put在很多人看来也是可以用来创建新的数据的,如果存在就修改,如果不存在就新建。
Request.CreateResponse(HttpStatusCode.NoContent); 这里只是返回了一个没有正文的响应。
类似的操作也可以使用Post来完成。public HttpResponseMessage Post(int id, Employee employee)
{
int index = list.ToList().FindIndex(e => e.Id == id);
if (index >= 0)
{
list[index] = employee;
return Request.CreateResponse(HttpStatusCode.NoContent);
}
return Request.CreateResponse(HttpStatusCode.NotFound);
}Request.CreateResponse(HttpStatusCode.NoContent); 找到记录,这里只是返回了一个没有正文的响应。
Request.CreateResponse(HttpStatusCode.NotFound); 代表不存在这个记录
本篇结束。
相关文章推荐
- ASP.Net Web-api 不可多得的零基础教程3
- ASP.Net Web-api 不可多得的零基础教程6
- ASP.Net Web-api 不可多得的零基础教程8
- Asp.Net Web-api 不可多得的零基础教程10
- ASP.Net Web-api 不可多得的零基础教程2
- ASP.Net Web-api 不可多得的零基础教程1
- Asp.Net Web-api 不可多得的零基础教程9
- ASP.Net Web-api 不可多得的零基础教程4
- [ASP.NET 教程] 一. Web基础
- C#实现多级子目录Zip压缩解压实例 NET4.6下的UTC时间转换 [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了 asp.Net Core免费开源分布式异常日志收集框架Exceptionless安装配置以及简单使用图文教程 asp.net core异步进行新增操作并且需要判断某些字段是否重复的三种解决方案 .NET Core开发日志
- ASP.NET WEB API构建基于REST风格
- asp.net core WebAPI实现CRUD
- ASP.NET Core MVC/WebAPi 模型绑定探索
- A Book Store Application Using AngularJS and Asp.Net Web Api
- (转)通过ASP.NET Web API + JQuery创建一个简单的Web应用
- Atlas基础教程——ASP.NET Ajax快速开发
- Asp.net 2.0 中将网站首页生成静态页的一个比较好的方法 - 清清月儿 .NET万花筒 Asp.net技术 Asp.net教程 Asp.net源码 Asp.net基础 Asp.net控件 Asp.net入门 - CSDNBlog
- ASP.NET Web Api Self Host大文件上传功能
- Introduction to the ASP.NET Web API
- [译]ASP.NET Core Web API 中使用Oracle数据库和Dapper看这篇就够了