2016-08-02 32 views
5

So einige teste ich meine mit Postman Routing und ich kann nicht scheinen, diesen Aufruf zu erhalten, gehen Sie durch:Web Api Optionale Parameter in der Mitte mit dem Attribut Routing

API Funktion

[RoutePrefix("api/Employees")] 
public class CallsController : ApiController 
{ 
    [HttpGet] 
    [Route("{id:int?}/Calls/{callId:int?}")] 
    public async Task<ApiResponse<object>> GetCall(int? id = null, int? callId = null) 
    { 
     var testRetrieve = id; 
     var testRetrieve2 = callId; 

     throw new NotImplementedException(); 
    } 
} 

Postman Anfragen

http://localhost:61941/api/Employees/Calls FUNKTIONIERT NICHT

Fehler:

{ 
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:61941/api/Employees/Calls'.", 
    "MessageDetail": "No action was found on the controller 'Employees' that matches the request." 
} 

http://localhost:61941/api/Employees/1/Calls WORKS

http://localhost:61941/api/Employees/1/Calls/1

Jede Idee funktioniert, warum ich nicht ein optionales zwischen meinem Präfix und dem benutzerdefinierten Route verwenden kann? Ich habe versucht, sie in eine benutzerdefinierte Route zu kombinieren, und das ändert nichts, jedes Mal, wenn ich versuche, die ID auszusortieren, verursacht es Probleme.

Antwort

6

Optionale Parameter am Ende der Route Vorlage sein muss. also was du versuchst zu tun, ist nicht möglich.

Attribute routing: Optional URI Parameters and Default Values

entweder Sie Ihre Route temaple ändern

[Route("Calls/{id:int?}/{callId:int?}")] 

oder eine neue Aktion erstellen

[RoutePrefix("api/Employees")] 
public class CallsController : ApiController { 

    //GET api/Employees/1/Calls 
    //GET api/Employees/1/Calls/1 
    [HttpGet] 
    [Route("{id:int}/Calls/{callId:int?}")] 
    public async Task<ApiResponse<object>> GetCall(int id, int? callId = null) { 
     var testRetrieve = id; 
     var testRetrieve2 = callId; 

     throw new NotImplementedException(); 
    } 

    //GET api/Employees/Calls 
    [HttpGet] 
    [Route("Calls")] 
    public async Task<ApiResponse<object>> GetAllCalls() { 
     throw new NotImplementedException(); 
    } 
} 
1

Eigentlich brauchen Sie nicht optionaler Parameter in Route benötigen

[Route("Calls")] 

oder Sie bestimmen die Route

[Route("Calls/{id:int?}/{callId:int?}")] 
public async Task<ApiResponse<object>> GetCall(int? id = null, int? callId = null) 
+0

Während das als separate Funktion funktioniert, ich bin auf der Suche alle 3 einzukapseln diese potentiellen Aufrufe unter 1 funktionieren, wenn ich das optionale Routing-Recht verstehe, sollte es möglich sein. – tokyo0709

2

ich die Route ändern, würde sich ändern:

[Route("Calls/{id:int?}/{callId:int?}")] 

und Fügen Sie das Attribut [FromUri] zu Ihren Parametern hinzu:

([FromUri]int? id = null, [FromUri]int? callId = null) 

Meine Testfunktion sieht wie folgt aus:

[HttpGet] 
[Route("Calls/{id:int?}/{callId:int?}")] 
public async Task<IHttpActionResult> GetCall([FromUri]int? id = null, [FromUri]int? callId = null) 
{ 
    var test = string.Format("id: {0} callid: {1}", id, callId); 

    return Ok(test); 
} 

ich es mit aufrufen kann:

https://localhost/WebApplication1/api/Employees/Calls 
https://localhost/WebApplication1/api/Employees/Calls?id=3 
https://localhost/WebApplication1/api/Employees/Calls?callid=2 
https://localhost/WebApplication1/api/Employees/Calls?id=3&callid=2