zoukankan      html  css  js  c++  java
  • ASP.NET MVC4 常见问题集

    1. HttpResponseMessage<T> 泛型取消

    We are working on a blog explaining the changes in HttpResponseMessage<T>, HttpRequestMessage<T>, and ObjectContent<T> in more details as we have simplified a bunch and made things more consistent.


    The reason for the changes was that we ran into some issues with the HttpResponseMessage<T> and HttpRequestMessage<T> model. First, it was not possible to guarantee that the <T> carried in the HttpContent in a request or response actually was a <T>. As requests and responses are completely mutable they can contain any HttpContent implementation without any regard to HttpResponseMessage<T> or HttpRequestMessage<T>.


    Second, HttpResponseMessage<T> and HttpRequestMessage<T> behaved slightly different depending on whether they were used on client side or server side.


    Third, we wanted to make the common case simpler so that you always see HttpRequestMessage and HttpResponseMessage instances and not have to worry about whether they were generic or not. Now they are always the same.


    Here’s a rough mapping from the old to the new model:


    On the client side, all you now do when wanting to PUT or POST JSON (or XML) is to use these methods exposed directly on HttpClient:


    HttpClient.PostAsJsonAsync<T>(T value) sends “application/json”

    HttpClient.PostAsXmlAsync<T>(T value) sends “application/xml”


    On the server side, in a controller you


    ApiController.Request.CreateResponse<T> (T) sends content in the best format determined by content negotiation


    You can also say: I want to send it in JSON explicitly in which case we will do that:


    ApiController.Request.CreateResponse<T> (T, “application/json”) sends content in “application/json”


    You can also cook up an ObjectContent yourself where you are in complete control of the formatter you want to use


    HttpResponseMessage response = new HttpResponseMessage();

    response.Content = new ObjectContent<T>(T, myFormatter, “application/some-format”);

    Hope this helps – as I said we will be sending out more details on this.

    public HttpResponseMessage PostReservation(Reservation reservation)
            {
                if (ModelState.IsValid)
                {
                    db.Reservations.Add(reservation);
                    db.SaveChanges();
    
                    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, reservation);
                    response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = reservation.ReservationID }));
                    return response;
                }
                else
                {
                    return Request.CreateResponse(HttpStatusCode.BadRequest);
                }
            }
    

      

  • 相关阅读:
    2017-2018-2 20179223《网络攻防技术》第七周作业
    2017-2018-2 20179223《网络攻防技术》第六周作业
    2017-2018-2 20179223《网络攻防技术》第五周作业
    2017-2018-2 20179223《密码与安全新技术》第二周作业
    2017-2018-2 20179223《网络攻防技术》第四周作业
    2017-2018-2 20179223《密码与安全新技术》第一周作业
    2017-2018-2 20179223《网络攻防技术》第三周作业
    2018-2018-2 《网络攻防》第二周作业
    51nod1270 数组的最大代价(简单dp)
    51nod1269 B君的圆锥
  • 原文地址:https://www.cnblogs.com/taoqianbao/p/2810381.html
Copyright © 2011-2022 走看看