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
Articles by Nizamuddin Siddiqui
Page 20 of 196
How to use indexers in C# 8.0?
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 MoreWhat is the use of ChildActionOnly attribute in ASP .Net MVC C#?
The ChildActionOnly attribute in ASP.NET MVC C# restricts an action method to be accessible only through child requests from views using Html.Action() or Html.RenderAction() helpers. It prevents direct URL access to the action method, making it ideal for creating reusable partial components. Syntax Following is the syntax for using the ChildActionOnly attribute − [ChildActionOnly] public ActionResult ActionName() { // action logic return View(); } To invoke a child action from a view, use one of these helper methods − @Html.Action("ActionName", new { parameter = value }) ...
Read MoreWhat are Async Streams in C# 8.0?
C# 8.0 introduces async streams, which enable asynchronous iteration over data that is generated or retrieved asynchronously. Unlike regular streams that return all data at once, async streams produce elements one at a time as they become available. Async streams use the IAsyncEnumerable interface and allow methods to use yield return with the async modifier. You can consume async streams using await foreach loops, which asynchronously wait for each element. Syntax Following is the syntax for declaring an async stream method − static async IAsyncEnumerable MethodName() { await SomeAsyncOperation(); ...
Read MoreWhat is Content Negotiation in Asp.Net webAPI C#?
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 MoreWhat are the advantages of using C# ASP.NET WebAPI?
ASP.NET Web API is a framework for building HTTP-based services that can be consumed by a broad range of clients including browsers, mobile applications, and desktop applications. It provides numerous advantages over traditional web services and other communication technologies. Key Advantages of ASP.NET Web API HTTP-Based Architecture Web API works seamlessly with HTTP protocols using standard HTTP verbs like GET, POST, PUT, and DELETE for CRUD operations. This makes it intuitive and follows REST principles − [HttpGet] public IActionResult GetUsers() { } [HttpPost] public IActionResult CreateUser([FromBody] User user) { } [HttpPut("{id}")] ...
Read MoreWhat is the use of Authorize Attribute in C# Asp.Net webAPI?
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 MoreHow to configure C# ASP.NET WebAPI in web.configure file?
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 MoreHow to consume Asp.Net WebAPI endpoints from other applications using C#?
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 MoreWhat is parameter binding in C# ASP.NET WebAPI?
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 MoreHow can we test C# Asp.Net WebAPI?
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