0% found this document useful (0 votes)
2 views

Attribute Based Routing in Core API

The document provides an overview of attribute-based routing in ASP.NET Core, detailing its importance for REST APIs and how to create an API using Visual Studio. It explains the differences between conventional and attribute routing, and includes a step-by-step guide for setting up a web API project. Additionally, it covers the creation of various GET endpoints and includes a quiz for reinforcement of the concepts discussed.

Uploaded by

Romina González
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Attribute Based Routing in Core API

The document provides an overview of attribute-based routing in ASP.NET Core, detailing its importance for REST APIs and how to create an API using Visual Studio. It explains the differences between conventional and attribute routing, and includes a step-by-step guide for setting up a web API project. Additionally, it covers the creation of various GET endpoints and includes a quiz for reinforcement of the concepts discussed.

Uploaded by

Romina González
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Attribute Based

Routing in Core API

#FullStackFuture

© NIIT-StackRoute
© NIIT-StackRoute 2022-2023
2022-2023
Outcome

After going through this video, you will learn


about:
● Routing Overview
● Creating API using Visual Studio
● Why to use attribute routing
● Test it using swagger

© NIIT-StackRoute 2022-2023
Routing Overview

● Routing is responsible for matching incoming HTTP requests and dispatching those
requests to the app's executable endpoints.
● The endpoint matching process can extract values from the request's URL and provide
those values for request processing.
● ASP.NET Core controllers use the Routing middleware to match the URLs of incoming
requests and map them to actions.

© NIIT-StackRoute 2022-2023
Routing Overview

● Conventional routing is typically used with controllers and views.


● Route templates are defined in Program.cs or in attributes
● Actions are either conventionally-routed or attribute-routed.
● Routing is based on the controller and action names only.

© NIIT-StackRoute 2022-2023
Routing Overview

app.MapControllerRoute(name: "default",
pattern: "{controller=Home}/{action=Details}/{id?}");

● it is an example of a conventional route.


● It's called conventional routing because it establishes a convention for URL paths

● The first path segment, maps to the controller name.


● The second segment, maps to the action name.
● The third segment, {id?} is used for an optional id.

© NIIT-StackRoute 2022-2023
Attribute Routing

● Placing a route on the controller or action makes it attribute-routed.


● Attribute routing is used with REST APIs.
● REST APIs should use attribute routing to model the app's functionality as a set of
resources where operations are represented by HTTP verbs.
● in program.cs, app.MapControllers(); is called to map attribute routed controllers.

© NIIT-StackRoute 2022-2023
Overview

● In this session we will create following API –


private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild",
"Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

© NIIT-StackRoute 2022-2023
Create a web API project

● From the File menu, select New > Project.


● Enter Web API in the search box.
● Select the ASP.NET Core Web API template and select Next.
● In the Configure your new project dialog, name the project AttribBasedRouting and
select Next.
● In the Additional information dialog:
● Confirm the Framework is .NET 6.0 (Long-term support).
● Confirm the checkbox for Use controllers(uncheck to use minimal APIs) is checked.
● Select Create.

© NIIT-StackRoute 2022-2023
Create a web API project

© NIIT-StackRoute 2022-2023
Create a web API project

© NIIT-StackRoute 2022-2023
Create a web API project

[ApiController]
[Route("api/[controller]")]
public class WeatherForecastController : ControllerBase
{

// https://localhost:7192/api/WeatherForecast
[HttpGet]
public string GetFirst()
{
return Summaries[0];
}

//https://localhost:7192/api/WeatherForecast/{id}
[HttpGet("{id}")]
public string GetAny(int id)
{
return Summaries[id - 1];
} © NIIT-StackRoute 2022-2023
Run and Test API

© NIIT-StackRoute 2022-2023
Run and Test API

© NIIT-StackRoute 2022-2023
Add another GET endpoint

// https://localhost:7192/api/WeatherForecast
[HttpGet]
public string GetLast()
{
return Summaries[Summaries.Length - 1];
}

© NIIT-StackRoute 2022-2023
Add another GET endpoint

© NIIT-StackRoute 2022-2023
Add another GET endpoint

// https://localhost:7192/api/WeatherForecast/last
[HttpGet(“last”)]
public string GetLast()
{
return Summaries[Summaries.Length - 1];
}

© NIIT-StackRoute 2022-2023
Run and Test API

© NIIT-StackRoute 2022-2023
Just a Minute
Q1. Which attribute is applied by default on controller when we create API?
a) Api
b) Http
c) ApiController
d) Controller
Ans - c

Q2. Which attribute is used on get method with id?


a) HtttpPost
b) HtttpGet
c) HtttpPut
d) HttpGet(“{id}”)
Ans - d

© NIIT-StackRoute 2022-2023
Recall

Recall

© NIIT-StackRoute 2022-2023
Thank You

#FullStackFuture

© NIIT-StackRoute
© NIIT-StackRoute 2022-20232022-2023

You might also like