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
Articles by Nizamuddin Siddiqui
Page 150 of 196
How can we inject the service dependency into the controller C# Asp.net Core?
ASP.NET Core injects objects of dependency classes through constructor or method by using built-in IoC container.The built-in container is represented by IServiceProvider implementation that supports constructor injection by default. The types (classes) managed by built-in IoC container are called services.In order to let the IoC container automatically inject our application services, we first need to register them with IoC container.Examplepublic interface ILog{ void info(string str); } class MyConsoleLogger : ILog{ public void info(string str){ Console.WriteLine(str); } }ASP.NET Core allows us to register our application services with IoC container, in the ConfigureServices method of the ...
Read MoreHow to enable Session in C# ASP.NET Core?
Session is a feature in ASP.NET Core that enables us to save/store the user data.Session stores the data in the dictionary on the Server and SessionId is used as a key. The SessionId is stored on the client at cookie. The SessionId cookie is sent with every request.The SessionId cookie is per browser and it cannot be shared between the browsers.There is no timeout specified for SessionId cookie and they are deleted when the Browser session ends.At the Server end, session is retained for a limited time. The default session timeout at the Server is 20 minutes but it is ...
Read MoreWhat is the difference between IApplicationBuilder.Use() and IApplicationBuilder.Run() C# Asp.net Core?
We can configure middleware in the Configure method of the Startup class using IApplicationBuilder instance.Run() is an extension method on IApplicationBuilder instance which adds a terminal middleware to the application's request pipeline.The Run method is an extension method on IApplicationBuilder and accepts a parameter of RequestDelegate.signature of the Run methodpublic static void Run(this IApplicationBuilder app, RequestDelegate handler)signature of RequestDelegatepublic delegate Task RequestDelegate(HttpContext context);Examplepublic class Startup{ public Startup(){ } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){ //configure middleware using IApplicationBuilder here.. app.Run(async (context) =>{ await context.Response.WriteAsync("Hello ...
Read MoreWhat is the use of "Map" extension while adding middleware to C# ASP.NET Core pipeline?
Middleware are software components that are assembled into an application pipeline to handle requests and responses.Each component chooses whether to pass the request on to the next component in the pipeline, and can perform certain actions before and after the next component is invoked in the pipeline.Map extensions are used as convention for branching the pipeline.The Map extension method is used to match request delegates based on a request’s path. Map simply accepts a path and a function that configures a separate middleware pipeline.In the following example, any request with the base path of /maptest will be handled by the ...
Read MoreWhat is the use of the Configure() method of startup class in C# Asp.net Core?
The configure method is present inside startup class of ASP.NET Core applicationThe Configure method is a place where you can configure application request pipeline for your application using IApplicationBuilder instance that is provided by the built-in IoC containerThe Configure method by default has these three parameters IApplicationBuilder, IWebHostEnvironment and ILoggerFactory .At run time, the ConfigureServices method is called before the Configure method. This is to register custom service with the IoC container which can be used in Configure method.IWebHostEnvironment :Provides information about the web hosting environment an application is running in.IApplicationBuilder:Defines a class that provides the mechanisms to configure an ...
Read MoreHow to run an external application through a C# application?
An external application can be run from a C# application using Process. A process is a program that is running on your computer. This can be anything from a small background task, such as a spell-checker or system events handler to a full-blown application like Notepad etc.Each process provides the resources needed to execute a program. Each process is started with a single thread, known as the primary thread. A process can have multiple threads in addition to the primary thread. Processes are heavily dependent on system resources available while threads require minimal amounts of resource, so a process is ...
Read MoreHow do we specify MIME type in Asp.Net WebAPI C#?
A media type, also called a MIME type, identifies the format of a piece of data. In HTTP, media types describe the format of the message body. A media type consists of two strings, a type and a subtype. For example −text/htmlimage/pngapplication/jsonWhen an HTTP message contains an entity-body, the Content-Type header specifies the format of the message body. This tells the receiver how to parse the contents of the message body.When the client sends a request message, it can include an Accept header. The Accept header tells the server which media type(s) the client wants from the server.Accept: text/html, application/xhtml+xml, ...
Read MoreHow to set a property having different datatype with a string value using reflection in C#?
Reflection is when managed code can read its own metadata to find assemblies. Essentially, it allows code to inspect other code within the same system. With reflection in C#, we can dynamically create an instance of a type and bind that type to an existing object. Moreover, we can get the type from an existing object and access its properties. When we use attributes in our code, reflection gives us access as it provides objects of Type that describe modules, assemblies, and types.Let us say we have a property of type double and in the runtime we actually have the ...
Read MoreHow can we get the client's IP address in ASP.NET MVC C#?
Every machine on a network has a unique identifier. Just as you would address a letter to send in the mail, computers use the unique identifier to send data to specific computers on a network. Most networks today, including all computers on the internet, use the TCP/IP protocol as the standard for how to communicate on the network. In the TCP/IP protocol, the unique identifier for a computer is called its IP address.Using HttpRequest.UserHostAddress propertyExampleusing System.Web.Mvc; namespace DemoMvcApplication.Controllers{ public class HomeController : Controller{ public string Index(){ string ipAddress = Request.UserHostAddress; ...
Read MoreWhat is the difference between | and || operators in c#?
|| is called logical OR operator and | is called bitwise logical OR but the basic difference between them is in the way they are executed. The syntax for || and | the same as in the following −bool_exp1 || bool_exp2bool_exp1 | bool_exp2Now the syntax of 1 and 2 looks similar to each other but the way they will execute is entirely different.In the first statement, first bool_exp1 will be executed and then the result of this expression decides the execution of the other statement.If it is true, then the OR will be true so it makes no sense to execute ...
Read More