Csharp Articles

Page 5 of 196

How to do versioning with custom media type in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 355 Views

API versioning through custom media types allows clients to specify which version of an API they want to use by including version information in the Accept header. This approach uses vendor-specific media types to route requests to the appropriate controller version based on the requested content type. Custom media types follow a pattern like application/vnd.company.resource.version+format, where the version is embedded within the media type identifier itself. Media Type Versioning Pattern The following media types route to different controller versions − application/vnd.demo.students.v1+json → StudentsV1Controller application/vnd.demo.students.v2+json → StudentsV2Controller Custom Media Type ...

Read More

How can we create a LOG filter for Logging purposes in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 1K+ Views

Action filters in C# ASP.NET Web API are used to add extra logic before or after action methods execution. The OnActionExecuting and OnActionExecuted methods allow you to inject custom logic at specific points in the request pipeline, making them perfect for logging purposes. A LOG filter helps track API method calls, execution times, and other diagnostic information. This is particularly useful for debugging, performance monitoring, and audit trails in production applications. Syntax Following is the syntax for creating a custom action filter by inheriting from ActionFilterAttribute − public class LogAttribute : ActionFilterAttribute { ...

Read More

What is Shallow Copy and how it is different from Deep Copy in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 1K+ Views

A shallow copy creates a new object but copies only the reference values from the original object. When the copied object contains reference types, both the original and copied objects point to the same memory locations for those inner objects. A deep copy creates a completely independent copy of an object, including all nested objects. Changes to one object do not affect the other since they occupy separate memory spaces. Shallow Copy vs Deep Copy Shallow Copy ...

Read More

How to sort a list of complex types using Comparison delegate in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 656 Views

Sorting a list of complex types in C# can be achieved using the Comparison delegate with the Sort() method. The List.Sort() method has an overload that accepts a Comparison delegate, allowing you to define custom sorting logic for complex objects. The Comparison delegate represents a method that compares two objects of the same type and returns an integer indicating their relative order. Syntax Following is the syntax for the Sort() method using a Comparison delegate − public void Sort(Comparison comparison) The comparison delegate signature is − public delegate int Comparison(T x, ...

Read More

How to write retry logic in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 1K+ Views

Retry logic is implemented to handle transient failures that may resolve themselves after a brief delay. This pattern is essential when working with network operations, database connections, or external APIs that may temporarily fail due to network issues, service overload, or temporary unavailability. It's important to log all connectivity failures that cause a retry so that underlying problems with the application, services, or resources can be identified. Implement retry logic only where you have the full context of a failing operation and when the operation is idempotent (safe to repeat). Syntax Following is the basic syntax for ...

Read More

How to use indexers in C# 8.0?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 168 Views

Indexers in C# allow objects to be accessed like arrays using the square bracket notation. C# 8.0 introduced the index from end operator (^) which provides a more intuitive way to access elements from the end of a collection or sequence. The ^ operator returns an index that is relative to the end of the sequence, making it the most compact and easiest way to access end elements compared to traditional methods like array.Length - 1. Syntax Following is the syntax for defining an indexer in a class − public returnType this[parameterType parameter] { ...

Read More

What is parameter binding in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

Parameter binding in ASP.NET Web API is the process of automatically mapping HTTP request data to controller action method parameters. Web API uses different binding strategies based on the parameter type and can be customized using attributes. Understanding how parameter binding works is essential for building robust Web APIs that can correctly receive and process data from HTTP requests. Default Parameter Binding Rules Web API follows these default rules for parameter binding − Simple types (int, bool, double, string, DateTime, GUID) are bound from the URI (route data or query string) Complex types (custom classes, ...

Read More

What are built-in message handlers in Asp.Net webAPI C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 847 Views

A message handler is a class that receives an HTTP request and returns an HTTP response. Message handlers derive from the abstract HttpMessageHandler class and provide us the opportunity to process, edit, or decline an incoming request before it reaches the HttpControllerDispatcher. Message handlers are executed much earlier in the request processing pipeline, making them ideal for implementing cross-cutting concerns in Web API. They form a chain of classes that process HTTP requests and responses through a pipeline. ASP.NET Web API Message Handler Pipeline ...

Read More

What is Content Negotiation in Asp.Net webAPI C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

Content negotiation in ASP.NET Web API is the process of selecting the best format for the response based on what the client can accept. When a client sends a request, it can specify its preferred response format using HTTP headers, and the server responds accordingly. The primary mechanism for content negotiation relies on several HTTP request headers that communicate the client's preferences to the server. HTTP Headers for Content Negotiation Accept − Specifies which media types are acceptable for the response, such as "application/json, " "application/xml, " or custom media types like "application/vnd.example+xml". Accept-Charset − Indicates ...

Read More

How to consume Asp.Net WebAPI endpoints from other applications using C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 471 Views

The HttpClient class provides a base class for sending and receiving HTTP requests and responses from URLs. It is a supported async feature of the .NET framework that can process multiple concurrent requests. HttpClient is available in the System.Net.Http namespace and acts as a layer over HttpWebRequest and HttpWebResponse. This article demonstrates how to consume ASP.NET Web API endpoints from external applications using HttpClient. We'll create a Web API with student data and then consume it from a console application. Creating the Web API Student Model First, let's define the Student model − namespace ...

Read More
Showing 41–50 of 1,951 articles
« Prev 1 3 4 5 6 7 196 Next »
Advertisements