
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How can we assign alias names for the action method in C# ASP.NET WebAPI?
A public method in a controller is called an Action method. Let us consider an example where DemoController class is derived from ApiController and includes multiple action methods whose names match with HTTP verbs like Get, Post, Put and Delete.
Example
public class DemoController : ApiController{ public IHttpActionResult Get(){ //Some Operation return Ok(); } public IHttpActionResult Post([FromUri]int id){ //Some Operation return Ok(); } public IHttpActionResult Put([FromUri]int id){ //Some Operation return Ok(); } public IHttpActionResult Delete(int id){ //Some Operation return Ok(); } }
Based on the incoming request URL and HTTP verbv (GET/POST/PUT/PATCH/DELETE), Web API decides which Web API controller and action method to execute e.g. Get() method will handle HTTP GET request, Post() method will handle HTTP POST request, Put() mehtod will handle HTTP PUT request and Delete() method will handle HTTP DELETE request for the above Web API. So, here the Url for the Get method will be http://localhost:58174/api/demo.
An alias name for an action method is provided by using ActionName attribute. Also it is necessary to change the route template in the WebApiConfig.cs.
Example
using DemoWebApplication.Models; using System.Collections.Generic; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class DemoController : ApiController{ [ActionName("FetchStudentsList")] public IHttpActionResult Get(){ List<Student> students = new List<Student>{ new Studen{ Id = 1, Name = "Mark" }, new Student{ Id = 2, Name = "John" } }; return Ok(students); } } }
Now we can call the Get() method with FetchStudentsList (alias name).
- Related Articles
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- How to use ViewBag in ASP .Net MVC C#?
- How can we test C# Asp.Net WebAPI?
- How to return custom result type from an action method in C# ASP.NET WebAPI?
- What is ViewData in ASP .Net MVC C#?
- How can we create a LOG filter for Logging purposes in C# ASP.NET WebAPI?
- What are the levels at which filters can be applied in ASP .Net MVC C#?
- Can we use MySQL keyword as alias name for a column?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- Set 'alias' for all the column names in a single MySQL query
- How can we assign a function to a variable in JavaScript?
- How can we create multiple MySQL triggers for the same trigger event and action time?
- How can we assign FOREIGN KEY constraint on multiple columns?
- Can we assign values to final arrays in Java?
