Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Few basic issue about ASP.Net Web Api Routing and Action method

$
0
0

i am new in web api. just reading article on it to get hold of this technology. i have good reputation in this site but apologized that i am going to post few basic question on web api routing and action method.

i was reading a write up from this url http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

1) what is the meaning of this kind of url

/customers/1/orders

2) how this kind of url

/customers/1/orders

is mapped to action. if possible how action method look like with attribute?

[Route("customers/{customerId}/orders")]
public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }

3) first see a sample code for routing

public class OrdersController : ApiController
{
    [Route("customers/{customerId}/orders")]
    [HttpGet]
    public IEnumerable<Order> FindOrdersByCustomer(int customerId) { ... }
}

now tell me how

FindOrdersByCustomer()

function will be invoke when we issue the below url

http://localhost/customers/1/orders
http://localhost/customers/bob/orders
http://localhost/customers/1234-5678/orders

how all above route will map to action method called

FindOrdersByCustomer

? please help me to understand this area.

4) Just do not understand the meaning of

AcceptVerbs("MKCOL")

what is

MKCOL

?

5) can we declare multiple post type action in a single web api controller ?

if possible then give me few sample code for multiple post type action in a web api controller.

6) see the code

[RoutePrefix("orders")]
public class OrdersController : ApiController
{
    [Route("{id:int}")] // constrained parameter
    public HttpResponseMessage Get(int id) { ... }

    [Route("details")]  // literal
    public HttpResponseMessage GetDetails() { ... }

    [Route("pending", RouteOrder = 1)]
    public HttpResponseMessage GetPending() { ... }

    [Route("{customerName}")]  // unconstrained parameter
    public HttpResponseMessage GetByCustomer(string customerName) { ... }

    [Route("{*date:datetime}")]  // wildcard
    public HttpResponseMessage Get(DateTime date) { ... }
}

These routes are ordered as follows.

orders/details
orders/{id}
orders/{customerName}
orders/{*date}
orders/pending

what does mean

RouteOrder = 1

?

what is the meaning of wild card in route

[Route("{*date:datetime}")] // wildcard

just do not understand route order in web api. can anyone elaborate with some easy sample to understand what it is and its significance.

7) Do not understand

To allow multiple HTTP methods for an action, or to allow HTTP methods other than GET, PUT, POST, and DELETE, use the AcceptVerbs attribute, which takes a list of HTTP methods.
public class ProductsController : ApiController
{
    [AcceptVerbs("GET", "HEAD")]
    public Product FindProduct(id) { }

    // WebDAV method
    [AcceptVerbs("MKCOL")]
    public void MakeCollection() { }
}
[AcceptVerbs("GET", "HEAD")]

i understand action

GET

but what is

Head

? what is this

[AcceptVerbs("MKCOL")]

please answer point wise if possible. thanks


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>