The Authorize attribute in C# ASP.NET Web API is a built-in authorization filter that controls access to API endpoints. It ensures that only authenticated and authorized users can access specific resources, returning HTTP 401 Unauthorized status for unauthenticated requests. Authorization occurs before the controller action method executes, giving you control over who can access your API resources. This attribute can be applied at different levels to provide flexible access control. Syntax Following is the basic syntax for applying the Authorize attribute − [Authorize] public class ControllerName : ApiController { // Controller actions ... Read More
To check if a value exists in a LinkedList in C#, you can use the Contains() method. This method performs a linear search through the linked list and returns true if the specified value is found, or false otherwise. Syntax Following is the syntax for the Contains() method − public bool Contains(T item) Parameters item − The value to locate in the LinkedList. Return Value Returns true if the item is found in the LinkedList; otherwise, false. LinkedList.Contains() Method ... Read More
ASP.NET Web API uses code-based configuration rather than XML-based configuration in web.config. While you cannot configure Web API routing and behavior directly in web.config, you can configure it programmatically in the WebApiConfig.cs file or during application startup. Configuration Location Web API configuration is typically done in the Register method of the WebApiConfig class, which is called during application startup − public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration goes here } } ... Read More
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
To check if an array contains elements that match specific conditions in C#, we can use the Array.Exists() method. This method returns true if at least one element in the array matches the specified condition, and false otherwise. Syntax Following is the syntax for Array.Exists() method − public static bool Exists(T[] array, Predicate match); Parameters array − The one-dimensional array to search. match − The predicate that defines the conditions of the elements to search for. Return Value Returns true if at least one element ... Read More
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
Testing ASP.NET Web API involves sending HTTP requests and receiving responses to verify that your API endpoints work correctly. There are several effective methods to test Web APIs, including using Swagger for interactive documentation and testing, and Postman for comprehensive API testing. Let us create a sample StudentController to demonstrate different testing approaches − Student Model namespace DemoWebApplication.Models { public class Student { public int Id { get; set; } public string Name { get; set; ... Read More
In C#, all arrays have a fixed size once they are created. You cannot change the length of an array after initialization. To check if an array has a fixed size, use the IsFixedSize property, which always returns true for arrays. Syntax Following is the syntax to check if an array has fixed size − bool isFixed = arrayName.IsFixedSize; Using IsFixedSize Property Example using System; public class Demo { public static void Main() { string[] products = new ... Read More
ViewData is a dictionary-based container in ASP.NET MVC that enables data transfer from Controller to View. It stores key-value pairs where keys are strings and values are objects, making it a flexible but loosely-typed data transfer mechanism. ViewData is valid only during the current HTTP request and provides one-way communication from controller to view. Since it uses string keys and object values, it requires explicit casting when retrieving data and does not provide compile-time type checking. Syntax Following is the syntax for storing data in ViewData − ViewData["keyName"] = value; Following is the ... Read More
The StringDictionary class in C# provides a collection of string key-value pairs. To remove all entries from a StringDictionary, you use the Clear() method, which empties the entire collection in a single operation. Syntax Following is the syntax for removing all entries from a StringDictionary − stringDictionary.Clear(); Parameters The Clear() method takes no parameters and returns void. It removes all key-value pairs from the StringDictionary and sets the Count property to zero. Using Clear() Method Example using System; using System.Collections; using System.Collections.Specialized; public class Demo { ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance