Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can we assign alias names for the action method in C# ASP.NET WebAPI?
In ASP.NET Web API, action methods are public methods in a controller that handle HTTP requests. By default, Web API maps action methods based on HTTP verbs (GET, POST, PUT, DELETE) or method names. However, you can assign alias names to action methods using the [ActionName] attribute, providing more descriptive and meaningful URLs.
Syntax
Following is the syntax for using the [ActionName] attribute −
[ActionName("AliasName")]
public IHttpActionResult MethodName() {
// method implementation
}
Default Action Method Naming
Without aliases, Web API maps methods based on HTTP verbs or conventional naming −
using System.Web.Http;
public class DemoController : ApiController {
public IHttpActionResult Get() {
// Handles GET requests
return Ok("GET method called");
}
public IHttpActionResult Post(int id) {
// Handles POST requests
return Ok("POST method called with id: " + id);
}
public IHttpActionResult Put(int id) {
// Handles PUT requests
return Ok("PUT method called with id: " + id);
}
public IHttpActionResult Delete(int id) {
// Handles DELETE requests
return Ok("DELETE method called with id: " + id);
}
}
In this example, the URL would be http://localhost:port/api/demo for GET requests.
Using ActionName Attribute
The [ActionName] attribute allows you to create more descriptive aliases for your action methods −
using System.Collections.Generic;
using System.Web.Http;
public class Student {
public int Id { get; set; }
public string Name { get; set; }
}
public class DemoController : ApiController {
[ActionName("FetchStudentsList")]
public IHttpActionResult Get() {
List<Student> students = new List<Student> {
new Student {
Id = 1,
Name = "Mark"
},
new Student {
Id = 2,
Name = "John"
}
};
return Ok(students);
}
[ActionName("CreateNewStudent")]
public IHttpActionResult Post([FromBody]Student student) {
// Logic to create new student
return Ok("Student created: " + student.Name);
}
[ActionName("UpdateStudentInfo")]
public IHttpActionResult Put(int id, [FromBody]Student student) {
// Logic to update student
return Ok("Student " + id + " updated to: " + student.Name);
}
}
With these aliases, the URLs become more descriptive:
GET /api/demo/FetchStudentsListPOST /api/demo/CreateNewStudentPUT /api/demo/UpdateStudentInfo/{id}
Route Configuration
To use action name aliases effectively, you may need to modify the route template in WebApiConfig.cs −
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Multiple Action Names for Same Method
You can also assign multiple aliases to a single action method using multiple [ActionName] attributes −
using System.Web.Http;
public class ProductController : ApiController {
[ActionName("GetProducts")]
[ActionName("FetchAllProducts")]
public IHttpActionResult Get() {
return Ok("Product list retrieved");
}
}
This allows the same method to be accessed via different URLs: /api/product/GetProducts or /api/product/FetchAllProducts.
Conclusion
The [ActionName] attribute in ASP.NET Web API allows you to create meaningful aliases for action methods, making URLs more descriptive and user-friendly. This approach improves API readability while maintaining clean separation between method names and URL routing.
