void 返回204状态码
HttpResponseMessage Convert directly to an HTTP response message.
IHttpActionResult Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message.
Other type Write the serialized return value into the response body; return 200 (OK).

1. void 返回204状态码

        public void Get()
{ }

2.直接转化成http响应消息

public HttpResponseMessage Get()
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
response.Content = new StringContent("hello", Encoding.Unicode);
response.Headers.CacheControl = new CacheControlHeaderValue()
{
MaxAge = TimeSpan.FromMinutes()
};
  //HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, new { a=1,b=2});
  //return response;
return response;

3.IHttpActionResult  调用 ExecuteAsync 创建HttpResponseMessage,最后实现 public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)方法

常用类https://msdn.microsoft.com/en-us/library/system.web.http.results(v=vs.118).aspx,也可以自定义实现IHttpActionResult接口。

 public IHttpActionResult Get()
{
return NotFound();//Ok()
//return new TextResult("hello", Request); } }
public class TextResult : IHttpActionResult
{
string _value;
HttpRequestMessage _request; public TextResult(string value, HttpRequestMessage request)
{
_value = value;
_request = request;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage()
{
Content = new StringContent(_value),
RequestMessage = _request
}; return Task.FromResult(response);
}
}

4.使用其他类型

public Product Get()
{
return new Product { Id = , Name = "我的商品" };
} }
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}

如果上述出现异常,无法返回404错误码,可以使用过滤器标签处理。

本文参考:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/action-results

WebAPI Action的几种返回值类型的更多相关文章

  1. C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解  ...

  2. WebApi 接口返回值类型详解 ( 转 )

    使用过Webapi的园友应该都知道,Webapi的接口返回值主要有四种类型 void无返回值 IHttpActionResult HttpResponseMessage 自定义类型 此篇就围绕这四块分 ...

  3. WebApi接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.作为程序猿,我们都知道参数和返回值是编程领域不可分割的两大块,此前分享了 ...

  4. WebApi 接口返回值不困惑:返回值类型详解。IHttpActionResult、void、HttpResponseMessage、自定义类型

    首先声明,我还没有这么强大的功底,只是感觉博主写的很好,就做了一个复制,请别因为这个鄙视我,博主网址:http://www.cnblogs.com/landeanfen/p/5501487.html ...

  5. (转)C# WebApi 接口返回值不困惑:返回值类型详解

    原文地址:http://www.cnblogs.com/landeanfen/p/5501487.html 正文 前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi ...

  6. [转]C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解

    本文转自:http://www.cnblogs.com/landeanfen/p/5501487.html 阅读目录 一.void无返回值 二.IHttpActionResult 1.Json(T c ...

  7. C#进阶系列——WebApi接口返回值类型详解

    阅读目录 一.void无返回值 二.IHttpActionResult 1.Json(T content) 2.Ok(). Ok(T content) 3.NotFound() 4.其他 5.自定义I ...

  8. Web Api 接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 WebApi 接口参数:传参详解,这篇博文内容本身很基础 ...

  9. ASP.NET Core中的Action的返回值类型

    在Asp.net Core之前所有的Action返回值都是ActionResult,Json(),File()等方法返回的都是ActionResult的子类.并且Core把MVC跟WebApi合并之后 ...

随机推荐

  1. 【C#】浅析C#中的日期处理

    1.字符串转化为日期 1.1第一种方式 使用 Convert.toDateTime 方法,该方法有很多重载方法,这里笔者就介绍两个常用的重载方法. 第一种: 使用: Convert.ToDateTim ...

  2. 安卓获取ListView、GridView等滚动的距离(高度)

    public int getScrollY() { View c = mListView.getChildAt(0); if (c == null) { return 0; } int firstVi ...

  3. Oracle 12C -- Invisible Columns

    在12C中,当一个列被定义为"不可见"的时候,没有直接访问该列的sql语句是无法看到"不可见列"的,显式引用"不可见列"的语句是可以访问和操 ...

  4. Python 爬虫 大量数据清洗 ---- sql语句优化

    . 问题描述 在做爬虫的时候,数据量很大,大约有五百百万条数据,假设有个字段是conmany_name(拍卖公司名称),我们现在需要从五百万条数据里面查找出来五十家拍卖公司, 并且要求字段 time( ...

  5. 使用maven生成可执行的jar包

    从pom的xsi中可以打开描述pom的schema: 可以看到pom中,project的结构: 默认的mvn install生成的jar是不带主类入口的,需要在maven-compile-plugin ...

  6. PHP读取超大日志文件

    打开一个17G的日志文件,都不吃力,除了占cpu之外,内存占用不多,如果直接fopen根本打不开 注:它是逐行读取的 foreach( glob( ngx_log. "/*.log" ...

  7. 使用组件构建Android应用程序

    原文链接:http://android.eoe.cn/topic/android_sdk 应用程序组件 Android's application framework lets you create ...

  8. ThinkPHP32 MODULE_ALLOW_LIST 存在的bug 不生效

    1)BUG: 假设存在Api Home Admin Member 模块.仅仅想让用户訪问 Api Home,不同意訪问Admin Member. 必须将Admin Member 写在 MODULE_D ...

  9. ASP.NET CORE下取IP地址

    先记下来,以后用上了直接来这复制 string ip1 = HttpContext.Request.Headers["X-Real-IP"]; //取IP,NGINX中的配置里要写 ...

  10. 外部程序启动App

    第一种:直接通过包名: Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.joyodream. ...