Csharp Articles

Page 60 of 196

How can we assign alias names for the action method in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

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 More

How can we restrict access to methods with specific HTTP verbs in C# ASP.NETnWebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

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 More

How to do Web API versioning with URI in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 824 Views

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 More

How can we create an exception filter to handle unhandled exceptions in C#nASP.NET WebAPI?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 529 Views

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

How to make use of both Take and Skip operator together in LINQ C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 3K+ Views

The Take and Skip operators in LINQ C# are powerful tools for data manipulation. The Skip operator skips over a specified number of elements from the beginning of a sequence, while the Take operator returns a specified number of elements from the beginning of a sequence. When used together, Skip and Take enable you to implement pagination and extract specific ranges of data from collections. This combination is particularly useful for scenarios like displaying search results in pages or processing data in chunks. Syntax Following is the syntax for using Skip operator − var result ...

Read More

How to create an array with non-default repeated values in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 843 Views

We can create an array with non-default repeated values using Enumerable.Repeat(). This method creates a sequence that contains one repeated value a specified number of times. It requires the System.Linq namespace to be included. Syntax Following is the syntax for using Enumerable.Repeat() − IEnumerable Enumerable.Repeat(T element, int count) Parameters element − The value to be repeated in the sequence. count − The number of times to repeat the value in the generated sequence. Return Value Returns an IEnumerable that contains a repeated value. To convert ...

Read More

How to force garbage collection in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

The garbage collector in C# automatically manages memory by removing unused objects. However, you can force garbage collection to run immediately using the GC.Collect() method, though this is generally not recommended due to performance implications. Forcing garbage collection should only be used in specific scenarios where you know large amounts of memory have been freed and want to clean up immediately, such as after closing a large document or completing a memory-intensive operation. Syntax Following is the syntax for forcing garbage collection − GC.Collect(); To collect specific generations − GC.Collect(int generation); ...

Read More

How to get the Unix timestamp in C#

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 7K+ Views

A Unix timestamp is a system for describing time as the number of seconds that have elapsed since January 1, 1970, at 00:00:00 UTC. This epoch time is widely used across different operating systems and programming languages for consistent time representation. Unix timestamps are particularly useful for storing dates in databases, comparing times across different time zones, and working with APIs that expect epoch time values. What is Unix Timestamp? The Unix timestamp represents time as a single integer value − the count of seconds since the Unix epoch (January 1, 1970, UTC). This makes it timezone-independent ...

Read More

What is if/then directives for debug vs release in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

In C#, conditional compilation directives like #if DEBUG allow you to include or exclude code based on the build configuration. Visual Studio provides two main build configurations: Debug mode for development and debugging, and Release mode for final production builds. The #if DEBUG directive enables code to execute only when compiled in Debug mode. In Release mode, this code is completely excluded from compilation, making it useful for debugging statements, logging, and development-only features. Syntax Following is the basic syntax for conditional compilation directives − #if DEBUG // Code executed only ...

Read More

What is the difference between Monitor and Lock in C#?

Nizamuddin Siddiqui
Nizamuddin Siddiqui
Updated on 17-Mar-2026 2K+ Views

Both Monitor and lock provide thread synchronization mechanisms in C#, but they serve different purposes. The lock statement is a simplified syntax that internally uses Monitor.Enter and Monitor.Exit with proper exception handling. Monitor offers more advanced features for complex threading scenarios. Syntax Following is the syntax for using lock statement − lock (lockObject) { // critical section code } Following is the syntax for using Monitor class − Monitor.Enter(lockObject); try { // critical section code } finally { Monitor.Exit(lockObject); } ...

Read More
Showing 591–600 of 1,951 articles
« Prev 1 58 59 60 61 62 196 Next »
Advertisements