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
Programming Articles
Page 55 of 2547
What are the different types of filters in C# ASP.NET WebAPI?
Filters in ASP.NET Web API provide a way to inject additional logic at different stages of request processing. They enable cross-cutting concerns such as authentication, authorization, logging, and caching without cluttering your controller logic. Filters can be applied declaratively using attributes or programmatically, offering flexibility in how you handle these concerns. Web API supports several types of filters, each serving a specific purpose in the request pipeline. Understanding these filter types helps you choose the right approach for implementing various functionalities in your API. Types of Filters Web API Filter Pipeline ...
Read MoreGet an IDictionaryEnumerator object in OrderedDictionary in C#
The OrderedDictionary class in C# provides the GetEnumerator() method to obtain an IDictionaryEnumerator object. This enumerator allows you to iterate through the key-value pairs while maintaining the insertion order of elements. The IDictionaryEnumerator is specifically designed for dictionary collections and provides access to both the Key and Value properties of each element during enumeration. Syntax Following is the syntax for getting an IDictionaryEnumerator from an OrderedDictionary − IDictionaryEnumerator enumerator = orderedDictionary.GetEnumerator(); Following is the syntax for iterating through the enumerator − while (enumerator.MoveNext()) { Console.WriteLine("Key = " + enumerator.Key + ", Value = " + enumerator.Value); } Return Value The GetEnumerator()
Read MoreGet the types nested within the current Type C#
To get the types nested within the current Type in C#, you can use the GetNestedTypes() method. This method returns an array of Type objects representing all nested types (classes, interfaces, structs, etc.) defined within a type. Syntax Following is the syntax for getting nested types − Type[] nestedTypes = type.GetNestedTypes(); To specify visibility, use the overload with BindingFlags − Type[] nestedTypes = type.GetNestedTypes(BindingFlags.Public | BindingFlags.NonPublic); Using GetNestedTypes() Method The GetNestedTypes() method retrieves all public nested types by default. Here's a basic example − Example using ...
Read MoreHow 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 ...
Read MoreGet object at the top of the Stack in C#
The Stack collection in C# follows the Last In, First Out (LIFO) principle. To get the object at the top of the stack without removing it, we use the Peek() method. This method returns the top element but keeps it in the stack, unlike Pop() which removes the element. Syntax Following is the syntax for the Peek() method − public T Peek() Return Value The Peek() method returns the object at the top of the stack of type T. It throws an InvalidOperationException if the stack is empty. ...
Read MoreHow to get Synchronize access to the StringCollection in C#
The StringCollection class in C# is not thread-safe by default. To achieve synchronized access in multi-threaded environments, you can use the SyncRoot property combined with a lock statement to ensure thread-safe operations. The SyncRoot property returns an object that can be used to synchronize access to the collection, preventing multiple threads from modifying or accessing the collection simultaneously. Syntax Following is the syntax for synchronized access to StringCollection − lock(stringCollection.SyncRoot) { // thread-safe operations on stringCollection } Why Synchronization is Needed In multi-threaded applications, multiple threads may try to ...
Read MoreHow can we restrict access to methods with specific HTTP verbs in C# ASP.NETnWebAPI?
In ASP.NET Web API, HTTP verbs define the actions that can be performed on resources. The primary HTTP verbs are GET, POST, PUT, PATCH, and DELETE, which correspond to read, create, update, and delete operations respectively. You can restrict access to specific action methods using HTTP verb attributes or by following naming conventions. ASP.NET Web API provides two main approaches to restrict method access: naming conventions and HTTP verb attributes. This ensures that your API endpoints respond only to intended HTTP methods, improving security and API design. HTTP Verbs and CRUD Operations HTTP Verb ...
Read MoreHow to do Web API versioning with URI in C# ASP.NET WebAPI?
Once a Web API service is made public, different client applications start using our Web API services. As the business grows and requirements change, we may have to change the services as well, but the changes to the services should be done in a way that does not break any existing client applications. This is when Web API versioning helps. We keep the existing services as is, so we are not breaking the existing client applications, and develop a new version of the service that new client applications can start using. One of the most common approaches to ...
Read MoreCheck whether the specified character has a surrogate code in C#
In C#, a surrogate is a character that represents a Unicode code point outside the Basic Multilingual Plane (BMP). Surrogate characters are used to encode Unicode characters that require more than 16 bits, using a pair of 16-bit values called high surrogate and low surrogate. The Char.IsSurrogate() method checks whether a character at a specified position in a string is a surrogate character. Syntax Following is the syntax for checking surrogate characters − bool result = Char.IsSurrogate(string, index); bool result = Char.IsSurrogate(char); Parameters string − The string containing the character ...
Read MoreHow to do versioning with the Querystring parameter in C# ASP.NET WebAPI?
Querystring parameter versioning in ASP.NET Web API allows different API versions to be accessed using a query parameter like ?v=1 or ?v=2. By default, the DefaultHttpControllerSelector cannot route to version-specific controllers, so we need to create a custom controller selector. This approach enables you to maintain multiple API versions simultaneously while using descriptive controller names like StudentsV1Controller and StudentsV2Controller. How It Works The default controller selection process looks for a controller named exactly as specified in the route (e.g., StudentsController). When you have version-specific controllers like StudentsV1Controller, the default selector fails and returns a 404 error. ...
Read More