Search code examples
c#asp.net-web-apiroutesasp.net-web-api-routing

How do I specify the route in WebApi


How do I specify the following URL format in the routing and access the variables (service id, Security id, writedataflag) in the following:

 https://<Domain Name or localhost<Port No>>api/<service Id>/<security id>/<writedataflag>

Solution

  • Configure the route template.

    The following example uses Attribute Routing on a controller action.

    //matches GET api/<service Id>/<security id>/<writedataflag> 
    [HttpGet]
    [Route("api/{service_id}/{security_id}/{writedataflag}")]
    public IHttpActionResult MyAction(int service_id, int security_id, string writedataflag){
        //...    
    }
    

    the parameter types can be changed to suit your needs. Also consider using parameter constraints.

    Read more about that here

    Attribute Routing in ASP.NET Web API 2