Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a request model in WebAPI GET request with route attribute?

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};
        }

The above code works well for me.

My question is is it possible to bind a request model in GET request ?

for example I have a request model like this

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

and i want my get request like this

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};
        }

I tried this , but every time i get null in my book (BookRequestModel )

like image 636
sudil ravindran pk Avatar asked Jun 06 '14 06:06

sudil ravindran pk


People also ask

How do I specify a route in Web API?

The default route template for Web API is "api/{controller}/{id}". In this template, "api" is a literal path segment, and {controller} and {id} are placeholder variables. When the Web API framework receives an HTTP request, it tries to match the URI against one of the route templates in the routing table.

What is model binding in Web API?

Model Binding is the most powerful mechanism in Web API 2. It enables the response to receive data as per requester choice. i.e. it may be from the URL either form of Query String or Route data OR even from Request Body. It's just the requester has to decorate the action method with [FromUri] and [FromBody] as desired.

What class can be used to generate links to routes in Web API?

In the previous section, we learned that Web API can be configured in WebApiConfig class. Here, we will learn how to configure Web API routes. Web API routing is similar to ASP.NET MVC Routing. It routes an incoming HTTP request to a particular action method on a Web API controller.


1 Answers

add [FromUri] and try again as below

[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};
            }

for more information :-

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

like image 111
Neel Avatar answered Sep 30 '22 19:09

Neel