Skip to main content

Posts

Showing posts with the label Asp.net Core

Web API crud operation in .Net 6

To create a Web API in .NET 6, you will need to perform the following steps: 1. Install the .NET 6 SDK: To install the .NET 6 SDK, you can use the following command: dotnet new -i Microsoft.AspNetCore.Blazor. Templates :: 6.0 . 0 -preview. 7.20480 . 1 2. Create a new Web API project: To create a new Web API project, you can use the following command: dotnet new webapi -o MyWebApi This will create a new Web API project in a directory called "MyWebApi". Add the necessary dependencies: To add the necessary dependencies for your Web API, you can use the following command: dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.SqlServer Set up the database: To set up the database for your Web API, you will need to create a model class that represents the data you want to store. For example, you might create a model class called "Product" with properties like "Name" and "Price". Next, you will need to c...

Apply JWT Access Tokens and Refresh Tokens in ASP .NET Core Web API

 To apply JWT (JSON Web Token) access tokens and refresh tokens in an ASP .NET Core Web API, you can follow these steps: Install the necessary NuGet packages: dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer dotnet add package Microsoft.AspNetCore.Authorization In the Startup.cs file, configure the JWT authentication scheme in the ConfigureServices method: services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = Configuration["Jwt:Issuer"], ValidAudience = Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"])) }; }); In the Config...

Variables in C#

In C#, a variable is a named storage location in the program's memory where a value can be stored and accessed. Variables have a specific data type, which determines the kind of value they can store. Here is an example of declaring and initializing some variables in C#: int x = 10; // integer variable double y = 3.14; // double-precision floating-point variable char z = 'A'; // character variable string s = "Hello"; // string variable bool b = true; // boolean variable The syntax for declaring a variable in C# is: dataType variableName; For example: int x; double y; char z; string s; bool b; You can also declare and initialize a variable in the same statement, as shown in the examples above. You can access the value of a variable by using its name. For example: int x = 10; int y = x + 5; // y will be 15 You can also change the value of a variable by assigning a new value to it: You can also change the value of a variable by ...

Interface in details in c#

 In the C# programming language, an interface is a template that defines a set of related methods, properties, and events that a class or struct can implement. An interface defines the signature of the methods, properties, and events, but does not provide any implementation for them. It is up to the class or struct that implements the interface to provide the implementation for the members defined in the interface. Interfaces are useful for defining common contracts that can be implemented by multiple classes or structs in a software system. For example, an interface might define a set of methods that are common to all classes that represent a particular type of data, such as a customer record. Any class that represents a customer record can then implement this interface and provide the implementation for the methods defined in the interface. To define an interface in C#, the keyword "interface" is used, followed by the name of the interface and the members that it defines. H...

Dependency injection

  Dependency injection is a technique for implementing the dependency inversion principle in software design. The dependency inversion principle states that the design of a software system should be such that high-level components (such as business logic) depend on abstractions, rather than on low-level details (such as specific implementations of data access logic). In practice, dependency injection means that when designing a class or module, instead of creating its dependencies directly, it receives them through its constructor or methods as parameters. This allows the dependencies to be easily swapped out for different implementations in different scenarios, such as when unit testing the class, or when running the application in different environments. For example, consider a class that represents a shopping cart in an online store. The shopping cart class might have a dependency on a database connection, which it uses to store information about the items in the cart. Instead o...

Injecting and Using Configuration in Asp.net core

Let's explore the relationship between this Razor Page and the Razor Page's Page Model because typically it is the Page Model that will perform data access and do all the hard work to put together the data that the Razor Page will display . Easy data source that I can access right now is my application configuration. One of the sources of configuration for this application is appsettings.json So, for example, if I add a Message property to this JSON file and I say that message is equal to Hello world from   appsettings!. let's say I want to display this message in my Razor Page, well the whole idea in the relationship between a Razor Page and its Page Model is that the Razor Page is strictly about display.   So something I can do in ASP.NET Core is give my PageModel a constructor, and my constructor can take a parameter of type IConfiguration. We need to bri...

Add / Edit the pages & add custom link in Asp.net core project

Default Asp.net core supports responsive design. when the view port becomes this narrow, the menu collapses into the hamburger icon here, which I can still click on to get to the menu. And this behaviour is provided by Bootstrap. So, in this project is Bootstrap version 3 I navigate to the different pages in this application, like the Contact page or the About page, these will go to URLs like /Contact and /About. And in our project, ASP.NET Core is mapping these requests to Razor Pages. I just want to point out that if you have the application running without the debugger attached, you should be able to make changes in the project, and just save files and come out and refresh the browser and see those changes reflected in the browser. After Refresh. We will get page updated. Now I want to make is to add a link into my menu to get to the products that we're going to be working with in this project. Let's add an anchor tag with the...

Creating the new Project in Asp.net Core

In this article, I'm going to be using Visual Studio to work on this project. Since .NET Core works across platforms, including Linux and Mac, you might be using a different environment, like Visual Studio Code or Visual Studio for the Mac.  I am using Visual Studio and our first order of business is going to be to create the AcmeCore application that we want to work on.   I want to create a New, Project. And from the .NET Core templates First of all Open visual studio , click on File Menu, select new project as below.   I'm going to be taken to a second screen where I can select the specific type of template that I want to use to create this application. I'm going to tell Visual Studio that I want to use the .NET Core Web framework Select the directory where you want to create the project and give project name After that you will be taken to the below screen where you can select ASP.NET Core 2.1 on top o...