Barış Kısır

Senior Software Developer

Navigation
 » Home
 » RSS

.NET Web API Custom Routing

25 Oct 2016 » csharp

By default, .NET Web api generates query string route for string inputs and url look like this

http://www.example.com/api/GetProducts?productName={productName}&productCategory={productCategory}

Its really annoying and hard to understand when you have too many parameters.

How to change that?

You need to specify custom url at the top of method.

Example

[Route("api/Products/GetProducts/{productName}/{productCategory}")]
public List<Product> GetProducts(string productName, string productCategory) 
	{
		...
	}

And you can even make version for api like

[Route("api/v1/Products/GetProducts/{productName}/{productCategory}")]
[Route("api/v2/Products/GetProducts/{productName}/{productCategory}")]