November 26th, 2016

  今天同屋的阿凯跑去上海跟冯俊伟他们聚会去了,我在酒店一个人待着,也到不无聊,打打游戏,中午吃了顿海鲜大餐,下午看电视洗澡洗衣服,就这么过去了,明天约好和来杭州的旧同事出去逛逛吃个饭什么的。杭州最近也真的是很冷了,白天才五六度,这次来也没带什么衣服,天天冻得嘶哈嘶哈的,后来终于明白这就是导致闹肚子的原因。
   去普华永道面试完也没消息了,虽然对那个让人生厌的面试官很讨厌,但是多少还是期待这次面试有下文的,也许是为了一次工作机会,也许是对自己能力的一种证明,下周再没消息就不再盼了,等出差回去好好找工作。
   有些事还是犹犹豫豫,不能干净利索,哎╮(╯▽╰)╭

November 19th, 2016

     今天是生日吼,这一天会觉得有那么一点点的放松,但是并没有怎么过,中午泡面凑合一顿,晚上一顿驴火。最近出差很忙,今天是周日,我周四才从杭州赶回来,在那边已经呆了三周了。这次回来主要是因为有一个面试,普华永道的,越临近日期反而开始期待。对于这次面试并没有什么准备。去了之后,环境不错的,面试一共两个人,第一个人主打技术,第二个人,妈嘞,问题非常刁钻,咄咄逼人,因为并没有思想准备所以一上来还是挺蒙的,至于结果如何,也不重要了,等出差完了再认真投简历吧。
      还一件事儿,就是一冲动买了罗技G29,很多游戏不配套啊,麻烦死了,折腾好久才能玩,但是玩起来的感觉真的just so so啊,好无聊,装了一个尘埃拉力,太难了!玩不起来!!现在已经被我装箱子,打算卖掉…哭😭花了钱也很伤心…..
       下面的图是楼顶拍的哟,很适合烧烤party,浙江信息院楼顶。
      

Web Api 2 中关于Action返回值的详细介绍

原创文章转载请注明出处

本文介绍WebApi2如何将action中的结果返回至Http输出中。
 
一个WebApi Controller可以返回以下任意一种类型:
   1.void
   2.HttpReponseMessage
   3.IHttpActionResult
   4.其他类型
 
依据不同返回类型,WebApi会选择不同的机制来创建Http Reponse
 

例子:
 
 
1.void
WebApi会简单的返回空消息,状态码为204
 
public class ValuesController : ApiController
{
    public void Post()
    {
    }
}
 
HTTP 返回:
HTTP/1.1 204 No Content
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 02:13:26 GMT
 
2.HttpResponseMessage
WepApi会直接将值写入到Http响应中去。你可以使用一些选项在输出之前对http响应做一些控制,例如,可以控制Cache-Control头。
public class ValuesController : ApiController
{
    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(20)
        };
        return response;
    }
}
 
Response:
HTTP/1.1 200 OK
Cache-Control: max-age=1200
Content-Length: 10
Content-Type: text/plain; charset=utf-16
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 08:53:35 GMT
hello
 
如果你将一个领域模型对象传递给CreateReponse方法,WebApi会自动调用media formatter将模型序列化后写入到Http响应中。
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;
}
WebApi根据Http请求中的Accept头来选择具体使用哪个序列化器,更多信息参见 Content Negotiation.
 
3.IHttpActionResult
该接口本质上,定义了一个HttpReponseMessage工厂,使用IHttpActionResult有一些优势如下:

  • 简化Controller的单元测试 unit testing 
  • 将创建Http响应的通用逻辑移动到单独的类中(Moves common logic for creating HTTP responses into separate classes)
  • 通过隐藏创建响应的底层代码,使Controller、aciton的变得更清晰。

 
该接口定义了一个方法ExecuteAsync,用来创建HttpReponseMessage实例
public interface IHttpActionResult
{
    Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken);
}

所以,当Action返回一个IHttpActionResult接口的时候,WebApi会调用ExecuteAsync方法生成HttpReponseMessage实例,然后HttpReponseMessage实例会将信息写入到Http响应中返回给用户。
下面是一个简单的继承IHttpActionResult接口,返回给客户端一个文本信息的例子:
继承:
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);
    }
}
Controller:
public class ValuesController : ApiController
{
    public IHttpActionResult Get()
    {
        return new TextResult(“hello”, Request);
    }
}
Response:
HTTP/1.1 200 OK
Content-Length: 5
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 08:53:35 GMT
hello
 
但是,通常情况下我们会使用System.Web.Http.Results命名空间中的IHttpActionResult实现类。并且ApiController的一些内建辅助类可以返回这些实现类。
在下面的例子中,如果没有找到对应的产品ID,则会调用ApiController.NotFound返回一个404的IHttpActionResult的实例。否则的话,调用ApiController.OK返回一个包含产品信息的200状态的结果。
public IHttpActionResult Get (int id)
{
    Product product = _repository.Get (id);
    if (product == null)
    {
        return NotFound(); // Returns a NotFoundResult
    }
    return Ok(product);  // Returns an OkNegotiatedContentResult
}
 
4.Other Return Types其他返回类型
对于其他返回类型,WepApi会调用media formatter来序列化对象,还记得前面讲的吗,WebApi会根据请求中ACCEPT头来自动选择formatter(Content Negotiation)。然后将序列化结果写入到响应信息中。
public class ProductsController : ApiController
{
    public IEnumerable<Product> Get()
    {
        return GetAllProductsFromDB();//将会序列化
    }
}

注意:因为返回的是自定义类型,所以你不能直接指定返回错误代码404等,但是你可以通过抛出HttpResponseException 异常来指定错误代码,更多信息请看
 Exception Handling in ASP.NET Web API.
 
请求信息:
GET http://localhost/api/products HTTP/1.1
User-Agent: Fiddler
Host: localhost:24127
Accept: application/json
 
相应信息:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/8.0
Date: Mon, 27 Jan 2014 08:53:35 GMT
Content-Length: 56
[{“Id”:1,”Name”:”Yo-yo”,”Category”:”Toys”,”Price”:6.95}]