Asp.Net WebApi接口返回值IHttpActionResult
WebApi是微软在VS2012 MVC4版本中绑定发行的,webapi2.0同mvc5发行的
webapi一共有以下接口返回值
1、void无返回值
2、IHttpActionResult
- Json(T content)
- Ok()、 Ok(T content)
- NotFound()
- 其他
- 自定义IHttpActionResult接口的实现
3、HttpResponseMessage
4、自定义类型
void无返回值
没有返回值,http状态码204。我感觉用处不大,一个好的api应该有返回值。
public class CeshiController : ApiController
{
public void PostSave()
{
//doing
}
}
IHttpActionResult
IHttpActionResult是Web API 2中引入的一个接口,IHttpActionResult是HttpResponseMessage的一个工厂类。IHttpActionResult是WebAPI中推荐的标准返回值,ApiController类中也提供了不少标准的工厂函数方便我们快速构建它们,如,Json,Ok,NotFound,BadRequest等。
1、Json<T>(T content)
比较常用的方法
namespace cms.Web.API
{
public class CeshiController : ApiController
{
public studentBLL bll = new studentBLL();
public IHttpActionResult GetList()
{
var list = bll.FindList();
return Json(list);
}
public IHttpActionResult GetList2()
{
dynamic data = new { name = "李白", age = };
return Json(data);
}
}
}
2、Ok()、 Ok<T>(T content)
比较常用的方法
namespace cms.Web.API
{
public class CeshiController : ApiController
{
public studentBLL bll = new studentBLL();
public IHttpActionResult GetList()
{
var list = bll.FindList();
return Ok(list);
}
public IHttpActionResult GetList2()
{
dynamic data = new { name = "李白", age = };
return Ok(data);
}
public IHttpActionResult PostAdd()
{
//表示不向客户端返回任何信息,只告诉客户端请求成功
return Ok();
}
public IHttpActionResult GetModel(int id)
{
var model = bll.Find(id);
return Ok(model);
}
}
}
3、NotFound()
比较常用的方法
找不到记录时,有时需要用到NotFound()方法
public IHttpActionResult GetModel(int id)
{
var model = bll.Find(id);
if (model == null)
{
return NotFound();
}
return Ok(model);
}
4、其他返回值
不常用
Content<T>(HttpStatusCode statusCode, T value)
public IHttpActionResult GetCeshi()
{
return Content(HttpStatusCode.OK, "hello webapi");
//return Ok("hello webapi")//一样的效果
}
BadRequest()
public IHttpActionResult GetCeshi(string name)
{
if (string.IsNullOrEmpty(name))
{
return BadRequest();
}
return Ok();
}
Redirect(string location)
public IHttpActionResult GetCeshi(string name)
{
return Redirect("https://www.cnblogs.com/webapi/");
}
HttpResponseMessage
这个对象也有它独特的使用场景,需要向客户端返回HttpResponse时就要用到这个对象。以下载为例,由于需要将下载的文件输出到客户端浏览器,Webapi的服务端需要向Web的客户端输出文件流,这个时候一般的IHttpActionResult对象不方便解决这个问题,于是HttpReponseMessage派上了用场。
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http; namespace cms.Web.API
{
public class CeshiController : ApiController
{public HttpResponseMessage GetFile()
{
try
{
var FilePath = System.Web.Hosting.HostingEnvironment.MapPath(@"~/upload/ceshi.zip");
var stream = new FileStream(FilePath, FileMode.Open);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Wep Api Demo ceshi.zip"
};
return response;
}
catch
{
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}
}
}
自定义类型
你可以将webapi的接口和普通方法一样,返回任意的类型,WebApi会自动序列化你自定义任何返回类型,然后将序列化的值写到响应正文里,状态码统一返回200。
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using cms.BLL;
using cms.Model; namespace cms.Web.API
{
public class CeshiController : ApiController
{
public studentBLL bll = new studentBLL(); public IEnumerable<student> GetList()
{
var list = bll.FindList();
return list;
} public student GetModel(int id)
{
var model = bll.Find(id);
if (model == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return model;
} public string GetMsg()
{
return "webapi";
} }
}
// IHttpActionResult是WebAPI中微软官方推荐的标准返回值,建议大家使用这个,跟着微软走错不了。
Asp.Net WebApi接口返回值IHttpActionResult的更多相关文章
- C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解 ...
- (转)C# WebApi 接口返回值不困惑:返回值类型详解
原文地址:http://www.cnblogs.com/landeanfen/p/5501487.html 正文 前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi ...
- [转]C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解
本文转自:http://www.cnblogs.com/landeanfen/p/5501487.html 阅读目录 一.void无返回值 二.IHttpActionResult 1.Json(T c ...
- WebApi 接口返回值不困惑:返回值类型详解。IHttpActionResult、void、HttpResponseMessage、自定义类型
首先声明,我还没有这么强大的功底,只是感觉博主写的很好,就做了一个复制,请别因为这个鄙视我,博主网址:http://www.cnblogs.com/landeanfen/p/5501487.html ...
- WebApi 接口返回值类型详解 ( 转 )
使用过Webapi的园友应该都知道,Webapi的接口返回值主要有四种类型 void无返回值 IHttpActionResult HttpResponseMessage 自定义类型 此篇就围绕这四块分 ...
- WebApi接口返回值不困惑:返回值类型详解
前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.作为程序猿,我们都知道参数和返回值是编程领域不可分割的两大块,此前分享了 ...
- C#进阶系列——WebApi接口返回值类型详解
阅读目录 一.void无返回值 二.IHttpActionResult 1.Json(T content) 2.Ok(). Ok(T content) 3.NotFound() 4.其他 5.自定义I ...
- ASP.Net WebAPI的返回值
Asp.Net WebAPI服务函数的返回值主要可以分为void.普通对象.HttpResponseMessag.IHttpActionResult e四种,本文这里简单的介绍一下它们的区别. 一.返 ...
- .Net Core 给WebApi接口返回值添加全局的日期格式化
public void ConfigureServices(IServiceCollection services) { services.AddMvc().AddJsonOptions(option ...
随机推荐
- 给有C或C++基础的Python入门 :Python Crash Course 5 if语句
本章就是Pyhon版的 if语句.原理大家都懂,就不一一说说明了. 值得注意的两点: 1. 在每个if类语句结尾必须加上符号“:”. 2. 注意,在python中是否缩进代表与上一行代码是否有关. 下 ...
- [Web]flask-excel实现excel文件下载的前后端实现
之前同事写了前端表格导出的功能, 前后端逻辑没有梳理, 导致后端代码十分臃肿. 接手之后, 重新选择了前端table插件, 从jqxGrid变更为bootstrapTable. 本来想依赖集成的tab ...
- BZOJ.4558.[JLOI2016]方(计数 容斥)
BZOJ 洛谷 图基本来自这儿. 看到这种计数问题考虑容斥.\(Ans=\) 没有限制的正方形个数 - 以\(i\)为顶点的正方形个数 + 以\(i,j\)为顶点的正方形个数 - 以\(i,j,k\) ...
- Android中,如何提升Layout的性能?
Layout 是 Android 应用中直接影响用户体验的关键部分.如果实现的不好,你的 Layout 会导致程序非常占用内存并且 UI 运行缓慢.Android SDK 带有帮助你找到 Layout ...
- CQD(陈丹琦)分治 & 整体二分——专题小结
整体二分和CDQ分治 有一些问题很多时间都坑在斜率和凸壳上了么--感觉斜率和凸壳各种搞不懂-- 整体二分 整体二分的资料好像不是很多,我在网上找到了一篇不错的资料: 整体二分是个很神的东西 ...
- 说一下Servlet里面得request和response
当一个servlet被调用的时候,我们一般继承带协议的httpServlet,大方向上是下图这样 在这里面request和response起了什么作用呢? 来细究一下. request:1.封装了客户 ...
- 生成缓存文件cache file
生成缓存文件cache file class Test{ public function index(){ $arrConfig = Array( 'name' => 'daicr', 'age ...
- jmeter接口测试实例1-添加学生信息
jmeter实例1:添加学生信息 进入jmeter,添加线程组改名称为添加学生信息(为了好区分接口),添加http请求,输入IP,方法,路径,在body data中输入json串,同上面postman ...
- 杭电acm2059-龟兔赛跑 java
一看题就知道是动态规划,不过这要看下如何设置变化数组了 先分析这道题:兔子到达终点的时间时固定的,因此只需要考虑乌龟了,乌龟骑电车和骑自行车的时间,然后计算,因为中间有N个充电站,可以看做N个点(到起 ...
- java大数BinInteger
当我们遇到long不行的时候就要考虑这个BinInteger了,因为这是只要你内存够大,就能输入很大的数,用这个处理高精度问题,是很容易的一件事,对于我这刚学java的萌新来说,长见识了,确实比C方便 ...