locked
Custom action name and web api RRS feed

  • Question

  • User264732274 posted

    how could i have Custom action name in web api ?

    what convention one need to main to have Custom action name in web api ?

    how to call custom action name with the help of jquery?

    suppose my action name would look like Employees(), Employee(int ID), AddEmployee(Employees Emp), UpdateEmployee(int x), RemoveEmployee(int x)

    and i want url should look like

    http:/api/Emp/Employees

    http:/api/Emp/Employee/1

    http:/api/Emp/AddEmployee/

    http:/api/Emp/UpdateEmployee/1

    http:/api/Emp/DeleteEmployee/1

    guide me what to do. thanks

    Wednesday, November 4, 2015 7:23 AM

Answers

  • User-219423983 posted

    Hi sudip_inn,

    1. what is the meaning of appending /orders ?

    The “orders” just is an URL mark and it shows the URL would call the action “GetOrdersByCustomer”. Here, it’s self-defined.

    2. how all above route will mapp to action method called FindOrdersByCustomer ?

    The “[HttpGet]” indicates the action “FindOrdersByCustomer” just could be accessed by “Get”. The “1”,” bob”,” 1234-5678” would be regard to the value of “customerId”, if you just want to match http://localhost/customers/1/orders, you could change [Route("customers/{customerId}/orders")] to [Route("customers/{customerId :int }/orders")] , for this you could click here.

    3. what is AcceptVerbs

    For this, you could refer to the following links. As it says, the convention applies only to GET, POST, PUT, and DELETE methods. You can enable other HTTP methods by using attributes on your controller.

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

    4. multiple post type action is possible. just see the code

    You code is explained the “Route Names” and it would generate a URL.

    5. do not understand route order-what does mean RouteOrder = 1 ?

    For this issue, you could first refer to the following thread to learn the routing convention, it is using the first route that is fully satisfied. Lower values are evaluated first. So the “orders/pending” would be last matched and the “orders/details” would be first matched.

    http://stackoverflow.com/questions/734249/asp-net-mvc-url-routing-with-multiple-route-values

    what is the meaning of wild card in route [Route("{*date:datetime}")]  // wildcard

    About the wildcard in web api routing, you could have a look at the following threads.

    http://stackoverflow.com/questions/7515644/infinite-url-parameters-for-asp-net-mvc-route

    http://stackoverflow.com/questions/19725634/wildcard-in-webapi-route-template

    I hope it’s useful to you.

    Best Regard,

    Weibo Zhang

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 5, 2015 4:09 AM

All replies

  • User1066278571 posted

    Use Attribute Routing for this:

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    Greetings Damien

    Wednesday, November 4, 2015 8:26 AM
  • User264732274 posted

    thanks for your link http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

    but do not understand many area. i like to discuss with you in details about those area.

    1) see this url

    /customers/1/orders

    how this kind of url is mapped to action. if possible how action method look like with attribute?

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

    what is the meaning of this kind of url

    /customers/1/orders

    ? if we see this part only

    /customers/1 and it has meaning like we want to see customer detail having id 1 but what is the meaning of appending /orders ?

    2) sample route and i do not understand how it would work

    Adding Route Attributes

    Here is an example of a route defined using an attribute:

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

    The string "customers/{customerId}/orders" is the URI template for the route. Web API tries to match the request URI to the template. In this example, "customers" and "orders" are literal segments, and "{customerId}" is a variable parameter. The following URIs would match this template:

    how all above route will mapp to action method called FindOrdersByCustomer ?

    please help me to understand this area.

    3) what is AcceptVerbs

    [Route("api/books")]
    [AcceptVerbs("MKCOL")]
    public void MakeCollection() { }

    just do not understand the meaning of AcceptVerbs("MKCOL") what is MKCOL ?

    4) multiple post type action is possible. just see the code

    public class BooksController : ApiController
    {
     
        [Route("api/books")]
        public HttpResponseMessage Post(Book book)
        {
            // Validate and add book to database (not shown)
    
            var response = Request.CreateResponse(HttpStatusCode.Created);
    
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("GetBookById", new { id = book.BookId });
            response.Headers.Location = new Uri(uri);
            return response;
        }
    }

    there is only one post type action defined in web api........can we define multiple post type action in web api controller?

    if possible show me how to write code for that.

    5) do not understand route order

    so tell me what is route order ?

    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

    please answer point wise in detail. thanks

    Wednesday, November 4, 2015 9:47 AM
  • User-219423983 posted

    Hi sudip_inn,

    1. what is the meaning of appending /orders ?

    The “orders” just is an URL mark and it shows the URL would call the action “GetOrdersByCustomer”. Here, it’s self-defined.

    2. how all above route will mapp to action method called FindOrdersByCustomer ?

    The “[HttpGet]” indicates the action “FindOrdersByCustomer” just could be accessed by “Get”. The “1”,” bob”,” 1234-5678” would be regard to the value of “customerId”, if you just want to match http://localhost/customers/1/orders, you could change [Route("customers/{customerId}/orders")] to [Route("customers/{customerId :int }/orders")] , for this you could click here.

    3. what is AcceptVerbs

    For this, you could refer to the following links. As it says, the convention applies only to GET, POST, PUT, and DELETE methods. You can enable other HTTP methods by using attributes on your controller.

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

    4. multiple post type action is possible. just see the code

    You code is explained the “Route Names” and it would generate a URL.

    5. do not understand route order-what does mean RouteOrder = 1 ?

    For this issue, you could first refer to the following thread to learn the routing convention, it is using the first route that is fully satisfied. Lower values are evaluated first. So the “orders/pending” would be last matched and the “orders/details” would be first matched.

    http://stackoverflow.com/questions/734249/asp-net-mvc-url-routing-with-multiple-route-values

    what is the meaning of wild card in route [Route("{*date:datetime}")]  // wildcard

    About the wildcard in web api routing, you could have a look at the following threads.

    http://stackoverflow.com/questions/7515644/infinite-url-parameters-for-asp-net-mvc-route

    http://stackoverflow.com/questions/19725634/wildcard-in-webapi-route-template

    I hope it’s useful to you.

    Best Regard,

    Weibo Zhang

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 5, 2015 4:09 AM
  • User264732274 posted

    thanks for answer but still many area is not clear.

    1) what is the meaning of this kind of url /customers/1/orders

    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) { ... }

    2) 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.

    3) Just do not understand the meaning of AcceptVerbs("MKCOL") what is MKCOL ?

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

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

    5) just do not understand route order. can please come with a easy sample to understand it. how order is done?

    thanks

    Thursday, November 5, 2015 4:21 AM