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
Server Side Programming Articles
Page 4 of 2110
How to fetch a property value dynamically in C#?
We can use Reflection to fetch a property value dynamically in C#. Reflection provides objects of type Type that describe assemblies, modules, and types, allowing us to dynamically access and manipulate object properties at runtime without knowing them at compile time. The System.Reflection namespace and System.Type class work together to enable dynamic property access through methods like GetProperty() and GetValue(). Syntax Following is the syntax for getting a property value dynamically − Type type = typeof(ClassName); PropertyInfo property = type.GetProperty("PropertyName"); object value = property.GetValue(instanceObject, null); Following is the syntax for setting a property ...
Read MoreHow to delete all files and folders from a path in C#?
Deleting all files and folders from a directory is a common task in C# file operations. The System.IO namespace provides the DirectoryInfo class and Directory class that offer multiple approaches to accomplish this task safely and efficiently. Syntax Following is the syntax for using DirectoryInfo to delete directories and files − DirectoryInfo di = new DirectoryInfo(path); di.Delete(true); // true = recursive delete Following is the syntax for using Directory class methods − Directory.Delete(path, true); // true = recursive delete Using DirectoryInfo for Recursive Deletion The DirectoryInfo class provides detailed ...
Read MoreHow to Add Items to Hashtable Collection in C#
A Hashtable collection in C# stores key-value pairs using a hash-based mechanism. Each key-value pair is organized based on the hash code of the key, which is calculated using a hash function. Unlike modern generic collections, hashtables can store objects of different data types but exhibit lower performance due to boxing and unboxing operations. In this article, we will explore how to add items to a hashtable collection using various methods available in the Hashtable class. Syntax The primary method for adding items to a hashtable is the Add() method − public virtual void Add(object ...
Read MoreWhat are the various return types of a controller action in C# ASP.NET WebAPI?
ASP.NET Web API controller actions can return different types depending on your application's requirements. Understanding these return types helps you choose the most appropriate approach for your specific scenarios. Available Return Types Void − Returns no content with HTTP 204 status Primitive/Complex Types − Returns data directly with automatic serialization HttpResponseMessage − Provides full control over the HTTP response IHttpActionResult − Offers a cleaner, testable approach to response creation Web API Return Type Evolution Void 204 No Content Primitive/ ...
Read MoreHow to return custom result type from an action method in C# ASP.NET WebAPI?
In ASP.NET Web API, you can create custom result types by implementing the IHttpActionResult interface. This interface provides a flexible way to customize HTTP responses beyond the standard return types like Ok(), BadRequest(), or NotFound(). The IHttpActionResult interface contains a single method that asynchronously creates an HttpResponseMessage instance, giving you full control over the HTTP response. Syntax The IHttpActionResult interface has the following structure − public interface IHttpActionResult { Task ExecuteAsync(CancellationToken cancellationToken); } How It Works When a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync ...
Read MoreHow to Check if a Key Exists in the Hashtable in C#?
The Hashtable class in C# represents a collection of key-value pairs organized based on the hash code of each key. In hashtables, keys are unique and non-null, while values can be null or duplicated. Often, you need to verify whether a specific key exists before performing operations like retrieval or updates. C# provides two methods to check if a key exists in a hashtable: ContainsKey() and Contains(). Both methods serve the same purpose but have slightly different naming conventions for clarity. Syntax Following is the syntax for the ContainsKey() method − public virtual bool ContainsKey(object ...
Read MoreHow to resolve CORS issue in C# ASP.NET WebAPI?
Cross-Origin Resource Sharing (CORS) is a security mechanism that uses additional HTTP headers to allow web applications running at one origin to access selected resources from a different origin. A web application executes a cross-origin HTTP request when it requests a resource that has a different origin (domain, protocol, or port) from its own. For example, consider an application with its front-end served from https://demodomain-ui.com and backend from https://demodomain-service.com/api. When the UI tries to make API calls to the backend, browsers restrict these cross-origin HTTP requests for security reasons, resulting in CORS errors. CORS ...
Read MoreWhat 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 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 MoreHow to do versioning with accept header in C# ASP.NET WebAPI?
The Accept header in HTTP tells the server what file format the client wants the response data in. These formats are commonly called MIME-types (Multipurpose Internet Mail Extensions). In ASP.NET Web API, you can use the Accept header to implement API versioning by including version information as a parameter in the Accept header. This approach allows you to maintain multiple API versions while using the same URL endpoints, routing requests to different controllers based on the version specified in the Accept header. How Accept Header Versioning Works When a client makes a request, it includes an Accept ...
Read More