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 16 of 196
How to create Guid value in C#?
A Globally Unique Identifier or Guid represents a 128-bit identification number that is mathematically guaranteed to be unique across multiple systems and distributed applications. The total number of unique keys (approximately 3.40282366×10³⁸) is so large that the probability of generating the same number twice is negligible. GUIDs are commonly used in applications where unique identification is critical, such as database primary keys, transaction IDs, or session identifiers. They are typically displayed as a sequence of hexadecimal digits like 3F2504E0-4F89-11D3-9A0C-0305E82C3301. Syntax The Guid structure is present in the System namespace. Following are the most common ways to create ...
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 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 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 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 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 can we create an exception filter to handle unhandled exceptions in C#nASP.NET WebAPI?
An exception filter in ASP.NET Web API provides a centralized way to handle unhandled exceptions that occur during controller method execution. When a controller method throws any unhandled exception (except HttpResponseException), the exception filter intercepts it and allows you to customize the HTTP response. Exception filters implement the System.Web.Http.Filters.IExceptionFilter interface. The simplest approach is to derive from the ExceptionFilterAttribute class and override the OnException method to define custom exception handling logic. Syntax Following is the basic syntax for creating a custom exception filter − public class CustomExceptionFilter : ExceptionFilterAttribute { public ...
Read More