zoukankan      html  css  js  c++  java
  • web api

    HttpResponseMessage 和  HttpResponseMessage

    HttpResponseMessage 用于处理返回状态信息

    HttpResponseMessage 用于发生错误信息时  404 505

    自定义返回状态

    1 public HttpResponseMessage dele(int id)
    2         {
    3             return Request.CreateResponse(HttpStatusCode.OK);
    4         }

    自定义状态码

     public HttpResponseMessage  dele(int id)
            {
                var resopnse = Request.CreateResponse(HttpStatusCode.OK);
                resopnse.StatusCode = HttpStatusCode.OK;
                resopnse.Content = new StringContent("状态值返回");
                return resopnse;
            }

    响应列举对象

     public IEnumerable<product> Get()
            {
                List<product> lp = new List<product>()
                {
                    new product() {name="xiaowang" },
                    new product() {name="xiaoli" }
                };
                return lp;
            }
          
           public HttpResponseMessage dele(int id)
            {
                HttpResponseMessage  message = new HttpResponseMessage(HttpStatusCode.OK );
                message.Content = new ObjectContent<IEnumerable<product>>(Get(), new JsonMediaTypeFormatter());
                return message;
            }

    CreateResponse<T> 泛型的回应方法

     public HttpResponseMessage dele(int id)
            {
                IEnumerable<product> ienu = Get();
                var pro = ienu.Where(p => p.name == "xiaowang");
                if (pro.FirstOrDefault<product>() != null)
                {
                    return Request.CreateResponse<product>(HttpStatusCode.OK, pro.First<product>());
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }

    自定义HttpResponseException

       public HttpResponseMessage dele(int id)
            {
                var message = new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent("错误消息"),
                    ReasonPhrase = "出错了"
                };
                throw new HttpResponseException(message);
    
            }
  • 相关阅读:
    linux输入yum后提示: -bash: /usr/bin/yum: No such file or directory的解决方案
    MySQL.报错2059处理方法
    抽象工厂模式的优缺点和适用场景
    字节与字符的区别
    Kubernetes诞生及历史
    k8s-设计理念-原理图
    JSF中的状态模式
    关于spring框架JdbcTemplate中的命令模式
    浅谈springMVC中的设计模式(1)——责任链模式
    Spring中的观察者模式
  • 原文地址:https://www.cnblogs.com/dh2014/p/5377595.html
Copyright © 2011-2022 走看看