Found 2628 Articles for Csharp

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

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:14:16

2K+ Views

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

What are the different types of filters in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 12:09:58

10K+ Views

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

How to resolve CORS issue in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:57:44

6K+ Views

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

How to return custom result type from an action method in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:54:42

1K+ Views

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

What are the various return types of a controller action in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:52:00

7K+ Views

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

What is the difference between FromBody and FromUri attributes in C# ASP.NETWebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:46:27

5K+ Views

When the ASP.NET Web API calls a method on a controller, it must set values for the parameters, a process called parameter binding.In order to bind a model (an action parameter), that would normally default to a formatter, from the URI we need to decorate it with [FromUri] attribute. FromUriAttribute simply inherits from ModelBinderAttribute, providing us a shortcut directive to instruct Web API to grab specific parameters from the URI using the ValueProviders defined in the IUriValueProviderFactory. The attribute itself is sealed and cannot be extended any further, but you add as many custom IUriValueProviderFactories as you wish.The [FromBody] attribute ... Read More

What is the difference between Task.WhenAll() and Task.WaitAll() in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:40:59

7K+ Views

The Task.WaitAll blocks the current thread until all other tasks have completed execution. The Task.WhenAll method is used to create a task that will complete if and only if all the other tasks have completed.If we are using Task.WhenAll we will get a task object that isn’t complete. However, it will not block but will allow the program to execute. On the contrary, the Task.WaitAll method call actually blocks and waits for all other tasks to complete.To understand with an example, let us say we have a task that performs some activity with the UI thread say some animation needs ... Read More

How to download a file from a URL in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:37:22

7K+ Views

A file can be downloaded from a URL using web client. It is available in System.Net namespace.The WebClient class provides common methods for sending data to or receiving data from any local, intranet, or Internet resource identified by a URI.The web client can be said as an application or web browser (like Google Chrome, Internet Explorer, Opera, Firefox, Safari) which is installed in a computer and used to interact with Web servers upon user’s request. It is basically a consumer application which collects processed data from servers.A Client and a Server are two parts of a connection, these are two ... Read More

How to populate XDocument from String in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:35:17

1K+ Views

XML is a self-describing language and it gives the data as well as the rules to identify what information it contains. Like HTML, XML is a subset of SGML - Standard Generalized Markup Language.The XDocument class contains the information necessary for a valid XML document. This includes an XML declaration, processing instructions, and comments.Note that we only have to create XDocument objects if we require the specific functionality provided by the XDocument class. In many circumstances, we can work directly with XElement. Working directly with XElement is a simpler programming model.XDocument derives from XContainer. Therefore, it can contain child nodes. ... Read More

How to get the name of the current executable in C#?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 11:31:26

3K+ Views

There are several ways to get the name of the current executable in C#.Using System.AppDomain −Application domain provides isolation between code running in different app domains. App Domain is a logical container for code and data just like process and has separate memory space and access to resources. App domain also serves as a boundary like process does to avoid any accidental or illegal attempts to access the data of an object in one running application from another.System.AppDomain class provides us the ways to deal with application domain. It provides methods to create new application domain, unload domain from memory ... Read More

Advertisements