Route Prefix VS Controller Name ( Web api )

Ahsan Attari picture Ahsan Attari · Jan 25, 2017 · Viewed 9.2k times · Source

I was wondering that if we use RoutePrefix attribute in our web api controller with a different name from controller's actual name. So would it work or not?

As far as i did

[RouterPrefix("quotation")]
public class SaleOrderController : ApiController { ... }

if we define RoutePrefix like above we can't access it via /quotation but we can access it using saleorder.

So what is RoutePrefix for or am i doing something wrong ?

Answer

Nkosi picture Nkosi · Jan 25, 2017

To use default route use Route("")

[RoutePrefix("quotation")]
public class SaleOrderController : ApiController {

    //GET quotation
    [Route("")]
    [HttpGet]
    public IHttpActionResult GetAll() { ... }

}

Source: Attribute Routing in ASP.NET Web API 2 : Route Prefix