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 14 of 196
How 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 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 MoreHow to do versioning with custom media type in C# ASP.NET WebAPI?
API versioning through custom media types allows clients to specify which version of an API they want to use by including version information in the Accept header. This approach uses vendor-specific media types to route requests to the appropriate controller version based on the requested content type. Custom media types follow a pattern like application/vnd.company.resource.version+format, where the version is embedded within the media type identifier itself. Media Type Versioning Pattern The following media types route to different controller versions − application/vnd.demo.students.v1+json → StudentsV1Controller application/vnd.demo.students.v2+json → StudentsV2Controller Custom Media Type ...
Read More