• 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);
    
            }
  • 相关阅读:
    三国杀三字经
    window.open参数表
    捕获FormRequest安全错误的类
    SqlServer之Convert 函数应用格式化日期
    数据库区分大小写的问题
    时间字符串的比较
    char码值对应列表大全
    web.config 连接字符串 加密解密,代码方式
    关于app_offline.htm的用法
    关闭窗口的时候弹出确认对话框
  • 原文地址:https://www.cnblogs.com/dh2014/p/5377595.html
Copyright © 2020-2023  润新知