Found 2628 Articles for Csharp

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:18:01

94 Views

We are creating two instances of the Employee class, e and e1. The e is assigned to e1. Both objects are pointing to the same reference, hence we will get true as expected output for all the Equals.In the second case we can observe that, even though properties values are same. Equals returns false. Essentially, when arguments are referring to different objects. Equals don’t check for the values and always returns false.Example 1class Program{    static void Main(string[] args){       Employee e = new Employee();       e.Name = "Test";       e.Age = 27;   ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 13:15:01

3K+ Views

The Take operator is used to return a given number of elements from an array and the Skip operator skips over a specified number of elements from an array.Skip, skips elements up to a specified position starting from the first element in a sequence.Take, takes elements up to a specified position starting from the first element in a sequence.Example 1class Program{    static void Main(string[] args){       List numbers = new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 1, 2, 3, 4, 5,  6, 7, 8, 9, 1, 2, 3, 5, 6, 7, ... Read More

What is Shallow Copy and how it is different from Deep Copy in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:59:31

867 Views

Shallow Copy −A shallow copy of an object copies the "main" object, but doesn’t copy the inner objects.The "inner objects" are shared between the original object and its copy.The problem with the shallow copy is that the two objects are not independent. If you modify the one object, the change will be reflected in the other object.Deep Copy −A deep copy is a fully independent copy of an object. If we copied our object, we would copy the entire object structure.If you modify the one object, the change will not be reflected in the other object.Exampleclass Program{    static void ... Read More

How can we create a LOG filter for Logging purposes in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:56:34

1K+ Views

Action filters are used to add extra logic before or after action methods execution. The OnActionExecuting and OnActionExecuted methods are used to add our logic before and after an action method is executed.Let us create create a LogAttribute that implemets ActionFilterAttribute which logs some information before and after action method execution.LogAttribute −Exampleusing System; using System.Diagnostics; using System.Web.Http.Controllers; using System.Web.Http.Filters; namespace DemoWebApplication.Controllers{    public class LogAttribute : ActionFilterAttribute {       public override void OnActionExecuting(HttpActionContext actionContext){          Debug.WriteLine(string.Format("Action Method {0} executing at {1}",             actionContext.ActionDescriptor.ActionName, DateTime.Now.ToShortDateString()),             ... Read More

How to do versioning with custom media type in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:52:13

218 Views

Media types allow an API to inform the client how to interpret the data in the payload. In the HTTP protocol, Media Types are specified with identifiers like text/html, application/json, and application/xml, which correspond to HTML, JSON, and XML respectively, the most common web formats. There are other more APIspecific Media Types as well, such as application/vnd.api+json.Below are versions that needs to be send in media types.application/vnd.demo.students.v1+json StudentsV1Controller application/vnd.demo.students.v2+json StudentsV2ControllerAdding our own CustomControllerSelector will fix the above error.CustomControllerSelector −Exampleusing System.Linq; using System.Net.Http; using System.Text.RegularExpressions; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; namespace WebAPI.Custom{    public class CustomControllerSelector : DefaultHttpControllerSelector{     ... Read More

How to do versioning with accept header in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:45:22

473 Views

The Accept header tells the server in what file format the browser wants the data. These file formats are more commonly called as MIME-types. MIME stands for Multipurpose Internet Mail Extensions.Versioning can be send in Headers like below.Version=1 StudentsV1Controller Version=2 StudentsV2ControllerSince we have not handled the version in accept headers, we are getting 404 not found error as we only have StudentV1 and StudentV2 controller. Let us add our own CustomControllerSelector which implements the DefaultHttpControllerSelector class.CustomControllerSelector −Exampleusing System.Linq; using System.Net.Http; using System.Web.Http; using System.Web.Http.Controllers; using System.Web.Http.Dispatcher; namespace WebAPI.Custom{    public class CustomControllerSelector : DefaultHttpControllerSelector{       private HttpConfiguration _config; ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:37:03

381 Views

An exception filter is executed when a controller method throws any unhandled exception that is not an HttpResponseException exception. The HttpResponseException type is a special case, because it is designed specifically for returning an HTTP response.Exception filters implement the System.Web.Http.Filters.IExceptionFilter interface. The simplest way to write an exception filter is to derive from the System.Web.Http.Filters.ExceptionFilterAttribute class and override the OnException method.Below is a filter that converts NotFiniteNumberException exceptions into HTTP status code 416, Requested Range Not Satisfiable.ExceptionFilterAttribute −Exampleusing System; using System.Net; using System.Net.Http; using System.Web.Http.Filters; namespace DemoWebApplication.Controllers{    public class ExceptionAttribute : ExceptionFilterAttribute{       public override void OnException(HttpActionExecutedContext ... Read More

How to do versioning with the Querystring parameter in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:32:59

316 Views

The DefaultHttpControllerSelector class in web api is responsible for selecting the appropriate controller action method that we send in the URI.Say we have to implement versioning in the query string like belowv=1 StudentsV1Controller (Version 1) v=2 StudentsV2Controller (Version 2)If we pass the versioning information in the query string like http://localhost:58174/api/student?v=1 will result in 404 Not Found error response because the SelectController() method which is present in DefaultHttpControllerSelector will look for the StudentsController but we have only StudentsV1Controller and StudentsV2Controller.To handle this case, we should add our own CustomControllerSelector which implements the DefaultHttpControllerSelector class.CustomControllerSelector −Exampleusing System.Net.Http; using System.Web; using System.Web.Http; using ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:26:20

611 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 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 option to implement versioning is by using URI. Below ... Read More

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:19:50

2K+ Views

The HTTP verbs comprise a major portion of our “uniform interface” constraint and provide us the action counterpart to the noun-based resource. The primary or mostcommonly-used HTTP verbs (or methods, as they are properly called) are POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively. There are a number of other verbs, too, but are utilized less frequently. Of those less-frequent methods, OPTIONS and HEAD are used more often than others.Action method can be named as HTTP verbs like Get, Post, Put, Patch or Delete. However, we can append any suffix ... Read More

Advertisements