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
A public method in a controller is called an Action method. Let us consider an example where DemoController class is derived from ApiController and includes multiple action methods whose names match with HTTP verbs like Get, Post, Put and Delete.Examplepublic class DemoController : ApiController{ public IHttpActionResult Get(){ //Some Operation return Ok(); } public IHttpActionResult Post([FromUri]int id){ //Some Operation return Ok(); } public IHttpActionResult Put([FromUri]int id){ //Some Operation return Ok(); } public IHttpActionResult Delete(int id){ ... Read More
Filters are used to inject extra logic at the different levels of WebApi Framework request processing. Filters provide a way for cross-cutting concerns (logging, authorization, and caching). Filters can be applied to an action method or controller in a declarative or programmatic way. Below are the types of filters in Web API C#.Authentication Filter −An authentication filter helps us to authenticate the user detail. In the authentication filter, we write the logic for checking user authenticity.Authorization Filter −Authorization Filters are responsible for checking User Access. They implement the IAuthorizationFilterinterface in the framework.Action Filter −Action filters are used to add extra ... Read More
Cross-Origin Resource Sharing (CORS) is a mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to 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, let us consider an application which is having its front end (UI) and back end (Service). Say the front-end is served from https://demodomain-ui.com and the backend is served from from https://demodomain-service.com/api. If an end user tries to access the application, for security ... Read More
We can create our own custom class as a result type by implementing IHttpActionResult interface. IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance.public interface IHttpActionResult { Task ExecuteAsync(CancellationToken cancellationToken); }If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.ExampleTo have our own custom result we must create a class that implements IHttpActionResult interface.using System.Net.Http; using System.Threading; using System.Threading.Tasks; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class CustomResult : IHttpActionResult{ string _value; HttpRequestMessage _request; ... Read More
Suppose we have an array A with n numbers and another input K, we have to find the index which will be the last to be reduced to zero after performing a given operation. The operation is explained as follows −Starting from A[0] to A[N – 1], update each element as A[i] = A[i] – K. Now, if A[i] < K then put A[i] = 0 and no further operation will be done on A[i] once it is 0.We have to repeat the operation until all of the elements are reduced to 0. And return the index which will be ... Read More
The Web API action method can have following return types.VoidPrimitive Type/Complex TypeHttpResponseMessageIHttpActionResultVoid −It's not necessary that all action methods must return something. It can have void return type.Exampleusing DemoWebApplication.Models using System.Web.Http; namespace DemoWebApplication.Controllers{ public class DemoController : ApiController{ public void Get([FromBody] Student student){ //Some Operation } } }The action method with void return type will return 204 No Content response.Primitive Type/Complex Type −The action method can return primitive type like int, string or complex type like List etc.Exampleusing DemoWebApplication.Models; using System.Collections.Generic; using System.Web.Http; namespace DemoWebApplication.Controllers{ public class ... Read More
Suppose we have an array A. In this array there are different numbers that occurs twice. But there is only one number that occurs once. We have to find that element from that array.Suppose A = [1, 1, 5, 3, 2, 5, 2], then the output will be 3. As there are each number twice, we can perform XOR to cancel out that element. because we know y XOR y = 0To solve this, we will follow these steps.Take one variable res = 0for each element e in array A, preform res := res XOR ereturn resExample Let us see the ... Read More
Suppose we have an array of N numbers, where each element in the array appears same number of times (m times, this is also given) except one element, We have to find this element.So, if the input is like A = [6, 2, 7, 2, 2, 6, 6], m = 3, then the output will be 7.To solve this, we will follow these steps −INT_SIZE := 8 * size of an integer type variableDefine an array count of size − INT_SIZE. and fill with 0for initialize i := 0, when i < INT_SIZE, update (increase i by 1), do:for initialize ... Read More
Suppose we have an array, we have to find an element before which all elements are less than it, and after which all are greater than it. Finally, return the index of the element, if there is no such element, then return -1.So, if the input is like A - [6, 2, 5, 4, 7, 9, 11, 8, 10], then the output will be 4.To solve this, we will follow these steps −n := size of arrmaximum_left := an array of size nmaximum_left[0] := -infinityfor i in range 1 to n, domaximum_left[i] := maximum of maximum_left[i-1], arr[i-1]minimum_right := infinityfor i ... Read More