
- 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 to return custom result type from an action method in C# ASP.NET WebAPI?
We can create our own custom class as a result type by implementing IHttpActionResult interface. IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance.
public interface IHttpActionResult { Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken); }
If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.
Example
To have our own custom result we must create a class that implements IHttpActionResult interface.
using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class CustomResult : IHttpActionResult{ string _value; HttpRequestMessage _request; public CustomResult(string value, HttpRequestMessage request){ _value = value; _request = request; } public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken){ var response = new HttpResponseMessage(){ Content = new StringContent($"Customized Result: {_value}"), RequestMessage = _request }; return Task.FromResult(response); } } }
Contoller Action −
Example
using DemoWebApplication.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class DemoController : ApiController{ public IHttpActionResult Get(int id){ List<Student> students = new List<Student>{ new Student{ Id = 1, Name = "Mark" }, new Student{ Id = 2, Name = "John" } }; var studentForId = students.FirstOrDefault(x => x.Id == id); return new CustomResult(studentForId.Name, Request); } } }
Here is the postman output of the endpoint which returns custom result.
- Related Articles
- How can we provide an alias name for an action method in Asp .Net MVC C#?
- How to do versioning with custom media type in C# ASP.NET WebAPI?
- How to use ViewBag in ASP .Net MVC C#?
- How can we assign alias names for the action method in C# ASP.NET WebAPI?
- What is ViewData in ASP .Net MVC C#?
- What are the various return types of a controller action in C# ASP.NET WebAPI?
- How to return an array from a method in Java?
- How to add custom message handlers to the pipeline in Asp.Net webAPI C#?
- What is the significance of NonActionAttribute in ASP .Net MVC C#?
- What is the use of ChildActionOnly attribute in ASP .Net MVC C#?
- How to implement an action listener using method reference in Java?
- Explain the custom value types in .NET
- How do we specify MIME type in Asp.Net WebAPI C#?
- How to return 2 values from a Java method
- Upgrading SAP .NET Connector from .NET 2.0 to .NET 3.0

Advertisements