---
title: "How to define routing in a .NET Core API?"  
description: "How to define routing in a .NET Core API?"  
author: "Steilla Mitchel"  
published: 2023-08-28  
updated: 2023-08-29  
canonical: https://www.mindstick.com/forum/159692/how-to-define-routing-in-a-dot-net-core-api  
category: ".net core"  
tags: ["routing", ".net core", ".net core api"]  
reading_time: 2 minutes  

---

# How to define routing in a .NET Core API?

How to [define](https://yourviews.mindstick.com/audio/1110/lifestyles-choices-that-define-our-lives) [routing](https://www.mindstick.com/articles/11998/attribute-routing-in-asp-dot-net-mvc) in a .NET [Core API](https://www.mindstick.com/forum/160547/how-to-pass-multiple-parameters-in-url-dot-net-core-api)?

## Replies

### Reply by Aryan Kumar

Routing in a .NET Core [API](https://www.mindstick.com/articles/12641/instagram-api-upgraded-to-facebook-graph) is the process of mapping HTTP requests to action methods in controllers. It is used to define how users can access your API.

There are two ways to define routing in a .NET Core API:

- **Attribute routing:** Attribute routing is the most common way to define routing in a .NET Core API. It uses attributes to map HTTP requests to action methods.
- **Conventional routing:** Conventional routing is a less common way to define routing in a .NET Core API. It uses conventions to map HTTP requests to action methods.

Here is an example of attribute routing in a .NET Core API:

C#

```plaintext
[ApiController]
[RoutePrefix("/products")]
public class ProductController : ControllerBase {

  [HttpGet]
  public IActionResult GetProducts() {
    // ...
  }

  [HttpPost]
  public IActionResult CreateProduct(Product product) {
    // ...
  }
}
```

In this example, the `GetProducts()` action method is mapped to the `/products` endpoint because it has the `HttpGet` attribute. The `CreateProduct()` action method is mapped to the `/products` endpoint because it has the `HttpPost` attribute.

Here is an example of conventional routing in a .NET Core API:

C#

```plaintext
[ApiController]
public class ProductController : ControllerBase {

  [Route("products")]
  public IActionResult GetProducts() {
    // ...
  }

  [Route("products", HttpMethod.Post)]
  public IActionResult CreateProduct(Product product) {
    // ...
  }
}
```

In this example, the `GetProducts()` action method is mapped to the `/products` endpoint because its method name matches the route name. The `CreateProduct()` action method is mapped to the `/products` endpoint because its method name matches the route name and the HTTP verb is `POST`.

Here are some additional tips for defining routing in a .NET Core API:

- Use attribute routing whenever possible. It is more flexible and easier to maintain than conventional routing.
- Use meaningful route names. This will help users to understand how to access your API.
- Use the `RoutePrefix` attribute to group related routes together. This will help to keep your URLs organized and easy to understand.
- Use the `ApiVersion` attribute to specify the API version for each route. This will help to ensure that your API is backward compatible.


---

Original Source: https://www.mindstick.com/forum/159692/how-to-define-routing-in-a-dot-net-core-api

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
