zoukankan      html  css  js  c++  java
  • web api 返回数据

    一、Webapi的接口返回值类型

    主要有四种类型 :void,HttpResponseMessage,IHttpActionResult,其他

    1. void

            [HttpGet]
            public void GetNone()
            {
    
            }

     返回空,状态码204

     2.HttpResponseMessage

     表示包括状态代码和数据的 HTTP 响应消息 类

     优势是可以灵活设置响应的各种参数

            [HttpGet]
            public HttpResponseMessage GetNone()
            {
                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, "value");
                response.Content = new StringContent("{'name':'tom'}", Encoding.Unicode);
                response.Headers.CacheControl = new CacheControlHeaderValue()
                {
                    MaxAge = TimeSpan.FromMinutes(20)
                };
                return response;
            }

     或者

    public HttpResponseMessage Get()
    {
        // Get a list of products from a database.
        IEnumerable<Product> products = GetProductsFromDB();
    
        // Write the list to the response body.
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, products);
        return response;
    }

     3.IHttpActionResult

    4.其他类型

    对其返回的所有类型的值都将序列化,并写入到body中,缺点是不能返回404,只能返回200

    但是可以引发HttpResponseException的错误代码

    二、

    1 返回null

    2.返回时间带T

    3.返回大小写问题

  • 相关阅读:
    temp12
    temp111
    test.c
    vipLogin.c
    services.c
    request.c
    managerLogin.c
    将博客搬至CSDN
    SpringMabatis整合项目mybatis-configuration.xml核心配置
    logback-test.xml配置文件模板
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/10287541.html
Copyright © 2011-2022 走看看