Generating links using route names in asp net web api



Text version of the video
http://csharp-video-tutorials.blogspot.com/2017/02/generating-links-using-route-names-in.html

Slides
http://csharp-video-tutorials.blogspot.com/2017/02/generating-links-using-route-names-in_21.html

All ASP .NET Web API Text Articles and Slides
http://csharp-video-tutorials.blogspot.com/2016/09/aspnet-web-api-tutorial-for-beginners.html

All ASP .NET Web API Videos

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

To generate links in ASP.NET Web API using route names
1. Set a name for the route using the Name property of the [Route] attribute

[Route(“{id:int}”, Name = “GetStudentById”)]
public Student Get(int id)
{
return students.FirstOrDefault(s =] s.Id == id);
}

2. Use the name of the route to generate the link

public HttpResponseMessage Post(Student student)
{
students.Add(student);
var response = Request.CreateResponse(HttpStatusCode.Created);
response.Headers.Location = new Uri(Url.Link(“GetStudentById”, new { id = student.Id }));
return response;
}

source