Loading

ASP.NET Core Web API

How to set up Multiple URLs for a Single Resource in ASP.NET Core Web API. The Complete ASP.NET Core Web API Developer Course 2022 [Videos].

In this Video, I am going to discuss How to set up Multiple URLs for a Single Resource in ASP.NET Core Web API Application with Examples. Please watch our previous Video, where we discussed how to work with Variables and Query Strings in ASP.NET Core Web API. We are going to work with the same application that we created in our Routing in ASP.NET Core Web API Video.

How to access a Single Resource with Multiple URLs in ASP.NET Core Web API:

Let us understand how to access a single resource with Multiple URLs with an example. Suppose, we have the following resource available in our Employee Controller.

How to access a Single Resource with Multiple URLs in ASP.NET Core Web API

Now we want to access the above resource with three different URLs such as Employee/All, AllEmployees, Employee/GetAll, then how we can do this? If this is our requirement, then we need to decorate the GetAllEmployees action method with three different Route attributes as shown in the below image.

How to set up Multiple URLs for a Single Resource in ASP.NET Core Web API

With this, now you can access the GetAllEmployees resource with three different URLs in the ASP.NET Core Web API Application. Let us prove this. First, modify the EmployeeController class as shown in the below code.

using Microsoft.AspNetCore.Mvc;
namespace RoutingInASPNETCoreWebAPI.Controllers
{
[ApiController]
public class EmployeeController : ControllerBase
{
[Route("Employee/All")]
[Route("AllEmployees")]
[Route("Employee/GetAll")]
public string GetAllEmployees()
{
return "Response from GetAllEmployees Method";
}
}
}

Now run the application and access the above resource using the different URLs as shown in the below image.

How to set up Multiple URLs for a Single Resource in ASP.NET Core Web API Application with Examples

As you can see in the above image, we are now able to access the same resource using three different URLs in ASP.NET Core Web API Application. As you can see each URL is unique. So, as long as the URLs are unique, you can access a particular resource using different URLs.

What happens if we use the same URL for multiple resources?

This is not accepted in ASP.NET Core Web API. Let us prove this with an example. Let say we have the following two resources.

What happens if we use the same URL for multiple resources?

Now, let us try to access both the above resources using the URL Employee/All, so what we will do is, we will decorate both the resources with the same Route Attribute as shown in the below image.

Accessing Multiple Resources using same URL in ASP.NET Core Web API

Let us modify the EmployeeController class as shown below.

using Microsoft.AspNetCore.Mvc;
namespace RoutingInASPNETCoreWebAPI.Controllers
{
[ApiController]
public class EmployeeController : ControllerBase
{
[Route("Employee/All")]
public string GetAllEmployees()
{
return "Response from GetAllEmployees Method";
}
[Route("Employee/All")]
public string GetEmployees()
{
return "Response from GetEmployees Method";
}
}
}

With the above changes in place, now run the application and navigate to Employee/All URL as shown in the below image.

Accessing Multiple Resources using same URL in ASP.NET Core Web API using Routing

As you can see in the above image, it is throwing an Internal Server Error saying AmbiguousMatchException: The request matched multiple endpoints. This is because the application finds to resource for the same URL and gets confused about who is going to handle the request and hence throwing the AmbiguousMatchException.

Note: So, the point that you need to remember is, each resource must have a unique URL, and also it is possible that a resource can be accessed using multiple URLs as long as all the URLs are unique. But it is not possible to access two or more different resources using a single URL in ASP.NET Core Web API Application.

Now change the Route Attribute of both the resources as shown in the below code to give different URLs.

using Microsoft.AspNetCore.Mvc;
namespace RoutingInASPNETCoreWebAPI.Controllers
{
[ApiController]
public class EmployeeController : ControllerBase
{
[Route("Employee/GetAll")]
[Route("EmployeeAll")]
public string GetAllEmployees()
{
return "Response from GetAllEmployees Method";
}
[Route("Employee/All")]
public string GetEmployees()
{
return "Response from GetEmployees Method";
}
}
}

With the above changes in place, now you can access the GetAllEmployees resource using two URLs i.e. Employee/GetAll and EmployeeAll. On the other hand, you can access the GetEmployees resource using the URL Employee/All as shown in the below image.

How to set up Multiple URLs for a Single Resource in ASP.NET Core Web API Application with Examples

In the next Video, I am going to discuss Token Replacement in ASP.NET Core Web API Attribute Routing with Examples. Here, in this Video, I try to explain How to set up Multiple URLs for a Single Resource in ASP.NET Core Web API Application with Examples. I hope you enjoy Multiple URLs for a Single Resource Video.

See All

Comments (621 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core Web API / Blog

Environment Setup for ASP.NET Core Web API Development

In this article, I am going to discuss the Environment Setup required for developing ASP.NET Core Web API Applications. Please read our previous article where we discussed HTTP Protocols. Tools and Software Requires for the development of ASP.NET Core Web API Applications.
8-Mar-2022 /43 /621

ASP.NET Core Web API / Blog

Creating ASP.NET Core Web API Project in Visual Studio 2019

In this article, I am going to discuss How to Create, Build, Run, and Test the ASP.NET Core Web API project in Visual Studio 2019. Please read our previous article, where we discussed How to Create, Build, Run, and Test ASP.NET Core Web API project using .NET Core CLI and Visual Studio Code.
8-Mar-2022 /43 /621

ASP.NET Core Web API / Blog

ASP.NET Core Web API Files and Folders

In this article, I am going to discuss the Files and Folders which are created by default when we create a new ASP.NET Core Web API Application. Please read our previous article where we discussed How to Create, Build, and Run ASP.NET Core Web API in Visual Studio 2019.
8-Mar-2022 /43 /621