2014-06-06 18 views
5
GET :http://www.Example.com/Api/1/0/Book/Company/0 

[Route("{UserId}/{Category}/books/{BookType}/{Page}")] 
     [HttpGet] 
     [RequestAuthorization] 
     public Response Get(int UserId,string Category, string BookType,int Page) 
     {   
      var books= this.contentService.GetUserItems(UserId,Category, BookType, Page) 
      return new Response() { Status = ApiStatusCode.Ok, Books = books}; 
     } 

Yukarıdaki kod benim için iyi çalışır. o GET isteği bir istek modeli bağlamak mümkünWebAPI GET isteğinde rota özniteliğinde bir istek modeli nasıl bağlanır?

Benim sorum nedir? (BookRequestModel

örneğin bu

public class BookbRequestModel 
    { 
     public int UserId { get; set; } 
     public int Category { get; set; } 
     public int Page { get; set; } 
     public string BookType { get; set; }  
    } 

gibi bir istek modeli var ve bu

GET :http://www.Example.com/Api/1/0/Book/Company/0 

to bind the data to my request model 


[Route("{UserId}/{Category}/books/{BookType}/{Page}")] 
     [HttpGet] 
     [RequestAuthorization] 
     public Response Get(BookbRequestModel book) 
     {   
      var books= this.contentService.GetUserItems(book.UserId,book.Category,book.BookType,book.Page) 
      return new Response() { Status = ApiStatusCode.Ok, Books = books}; 
     } 

gibi benim olsun isteği Bunu denedim istiyorum, ama her zaman benim kitapta boş olsun)

cevap

6

[FromUri] ekleyin ve aşağıdaki gibi tekrar deneyin

[Route("{UserId}/{Category}/books/{BookType}/{Page}")] 
      [HttpGet] 
      [RequestAuthorization] 
      public Response Get(([FromUri] BookbRequestModel book) 
      {   
       var books= this.contentService.GetUserItems(book.UserId,book.Category,book.BookType,book.Page) 
       return new Response() { Status = ApiStatusCode.Ok, Books = books}; 
      } 
Daha fazla bilgi için

: -

http://www.c-sharpcorner.com/UploadFile/2b481f/parameter-binding-in-Asp-Net-web-api/

+0

Merak etme “FromUri” ve “FromBody” parçasının bir parçasına sahip olabilir misiniz? Ben 'FromBody' modeli ve bir dize, model doğrulanır ve ben' ModelState' kontrol edebilirsiniz var bir sorun var, ama başlıbaşına dizesi doğrulamak gerekir. ParameterBindingAttribute, modeldeki özelliklere yerleştirilebilir mi? –

-3

onlar bunu yapamaz gibi bu parametreleri alır. RouteConfig'i değiştirmeyi denemenizi tavsiye ederim. Yeni bir rota ekleyin. WebApiConfig.cs.

config.Routes.MapHttpRoute(
       name: "NewApiRoute", 
       routeTemplate: "myapi/{Controller}/{id}", 
       defaults: new { id = new object()//this is to make it generic you can pass object of your class also } 
       ); 
+0

Lütfen, cevabı bilmiyorsanız, en azından diğer cevapları okuyun. – Dementic

İlgili konular