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

  • Question

  • User264732274 posted

    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


    2) how this kind of url


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

    public IEnumerable<Order> GetOrdersByCustomer(int customerId) { ... }

    3) first see a sample code for routing

    public class OrdersController : ApiController
        public IEnumerable<Order> FindOrdersByCustomer(int customerId) { ... }

    now tell me how


    function will be invoke when we issue the below url


    how all above route will map to action method called


    ? please help me to understand this area.

    4) Just do not understand the meaning of


    what is



    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

    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.


    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
        public void MakeCollection() { }
    [AcceptVerbs("GET", "HEAD")]

    i understand action


    but what is


    ? what is this


    please answer point wise if possible. thanks

    Thursday, November 5, 2015 1:41 PM


  • User753101303 posted

    It might be best to not ask too much in a single post. It will be easier to stop for what you understood or to keep discussing when you need more information. Also it is easier for others to answer to what they know best etc... etc... If we end up discussing too much things at the same time it will be harder to follow.

     In short :

    1: it is a way to ask the website "show me all orders for Customer 1"

    2: this is precisely what explains the article. The attribute allows to tell that when the pattern looks like customers/<something>/orders it should call this method (GetOrdersByCustomer)

    3: if I remember we discussed that already. Basically a piece of code can inspect programming elements at runtime, inspect attributes and ultimately create object and invoke methods. So you have a piece of code in MVC, that inspect the incoming request and find the matching action based on this attribute. You can write code that inspect other code elements.

    4: you likely know GET and POST. It is just another "http verb" intended to create a "collection" (ie a "folder"). It allows to define yet another criteria to tell if the icnming request should be mapped to this particular action

    5: try http://forums.asp.net/t/1815996.aspx?Web+Api+with+Multiple+Post+in+one+Controller. not sure to be able to quickly find a scenario where it is obviusly needed. IMO it will be always the time to ask if you ever come accross a case where you need it.

    6a: as explained it allows to tell in which order you want to check those routes (the first one that match is taken so you may want to prioritize that... once again if you REALLY need)
    6b:* should mean you could have other "segments" before  (ie it could be reports/<a date value> or whatever/<a date value>)

    7:MKCOL was discussed previously. HEAD is like GET except the content is not returned (for example you should be able to use that to check that something exists without actually downloading it). As this is not widely used, they mean that by design GET, PUT, POST, DELETE are allowed and that if you ever want to allow other HTTP verbs or to map multiple verbs to an action, then you need to explicitely allow that using the "AcceptVerbs" attribute. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 5, 2015 2:22 PM