2014-06-19 8 views
6

Sto cercando di ottenere questa risposta JSON con un'intestazione Ihttpstatus che riporta il codice 201 e mantenere IHttpActionResult come metodo di restituzione del metodo.Come restituire da un metodo IHttpActionResult una variabile personalizzata?

JSON che voglio tornato:

{ "CustomerID": 324}

mio metodo:

[Route("api/createcustomer")] 
[HttpPost] 
[ResponseType(typeof(Customer))] 
public IHttpActionResult CreateCustomer() 
{ 
    Customer NewCustomer = CustomerRepository.Add(); 
    return CreatedAtRoute<Customer>("DefaultApi", new controller="customercontroller", CustomerID = NewCustomer.ID }, NewCustomer); 
} 

JSON restituita:

"ID" : 324, "Data": "201 4-06-18T17: 35: 07.8095813-07: 00" ,

Ecco alcune delle dichiarazioni che ho provato che o mi ha dato URI errore nullo o mi hanno dato una risposta simile a quella sopra l'esempio .

return Created<Customer>(Request.RequestUri + NewCustomer.ID.ToString(), NewCustomer.ID.ToString()); 
return CreatedAtRoute<Customer>("DefaultApi", new { CustomerID = NewCustomer.ID }, NewCustomer); 

Con un metodo di tipo httpresponsemessage questo può essere risolto come illustrato di seguito. Tuttavia voglio usare un IHttpActionResult:

public HttpResponseMessage CreateCustomer() 
{ 
    Customer NewCustomer = CustomerRepository.Add(); 
    return Request.CreateResponse(HttpStatusCode.Created, new { CustomerID = NewCustomer.ID }); 
} 

risposta

9

questo modo si ottiene il risultato:

[Route("api/createcustomer")] 
[HttpPost] 
//[ResponseType(typeof(Customer))] 
public IHttpActionResult CreateCustomer() 
{ 
    ... 
    string location = Request.RequestUri + "/" + NewCustomer.ID.ToString(); 
    return Created(location, new { CustomerId = NewCustomer.ID }); 
} 

Ora il ResponseType non corrisponde. Se hai bisogno di questo attributo, dovrai creare un nuovo tipo di reso invece di usare un tipo anonimo.

public class CreatedCustomerResponse 
{ 
    public int CustomerId { get; set; } 
} 

[Route("api/createcustomer")] 
[HttpPost] 
[ResponseType(typeof(CreatedCustomerResponse))] 
public IHttpActionResult CreateCustomer() 
{ 
    ... 
    string location = Request.RequestUri + "/" + NewCustomer.ID.ToString(); 
    return Created(location, new CreatedCustomerResponse { CustomerId = NewCustomer.ID }); 
} 

Un altro modo per farlo è quello di utilizzare il DataContractAttribute sulla classe Customer per controllare la serializzazione.

[DataContract(Name="Customer")] 
public class Customer 
{ 
    [DataMember(Name="CustomerId")] 
    public int ID { get; set; } 

    // DataMember omitted 
    public DateTime? Date { get; set; } 
} 

Poi basta riportare il modello realizzato

return Created(location, NewCustomer); 
// or 
return CreatedAtRoute<Customer>("DefaultApi", new controller="customercontroller", CustomerID = NewCustomer.ID }, NewCustomer); 
+0

Grazie. Questa è stata una buona risposta. Ma non capisco perché "Created()" richiede la posizione e quello che è. – user3754602

+0

Aggiunge un'intestazione 'Location' alla risposta contenente l'URL all'entità appena creata. – Jasen

Problemi correlati