Setting session in Asp.Net Core 7.0 MVC project

I am using Visual Studio 2022

To Set the session in Asp.Net Core 7.0 MVC project, do the following changes :- ___________________________________________________________________________________

In Program.cs file:-


using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Hosting;

using Microsoft.AspNetCore.Razor.Language.Intermediate;


var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllersWithViews();

builder.Services.AddSession();

var app = builder.Build();

if (!app.Environment.IsDevelopment())

{

    app.UseExceptionHandler("/Home/Error");

    app.UseHsts();

}

app.UseSession();

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(

name: "default",

pattern: "{controller=HR}/{action=HRLogin}/{id?}");

app.Run();
____________________________________________

In Controller:-

HttpContext.Session.SetString("username", login.Username.ToString());

then call the session value

string SessionName = HttpContext.Session.GetString("username");

Cross-Origin Requests (CORS) in ASP.NET Core

CORS is a security feature that prevents malicious websites from making unauthorized requests to a different domain. ASP.NET Core provides an easy way to configure CORS for web applications by using the Microsoft.AspNetCore.Cors package. Developers can enable CORS for all origins, headers, and methods or configure CORS to only allow specific origins, headers, and methods.

An example of a CORS rules:
app.UseCors(builder =>
        builder
        .WithOrigins("http://domain.com")
        .AllowAnyMethod()
        .AllowAnyHeader());

To enable CORS in ASP.Net Core Web API, these are the steps we need to follow :-
- Install the CORS middleware.
- Register CORS middleware to the pipeline in the ConfigureServices method of Startup.cs.
- Enable CORS in the Configure method of Startup.cs.
- Enable/Disable CORS in the controllers, the action methods, or globally.

How to Find Nth Highest Salary in SQL

 SELECT * FROM(

SELECT emp_name, salary, DENSE_RANK() 

over(ORDER BY salary DESC) AS ranking FROM employee) AS k

WHERE ranking=2;



SELECT * FROM(

SELECT emp_name, salary, ROW_NUMBER() 

over(ORDER BY salary DESC) AS ranking FROM employee) AS k

WHERE ranking=2;



SELECT * FROM(

SELECT emp_name, salary, RANK() 

over(ORDER BY salary DESC) AS ranking FROM employee) AS k

WHERE ranking=2;


Web API vs Rest API

 1) Web API vs REST API: Protocol

Web API supports protocol for HTTP/s protocol and URL requests/responses headers that enable services to reach various clients through the web. On the other hand, all communication in the REST API is supported only through HTTP protocol.


2) Web API vs REST API: Formats

Although APIs perform identical tasks, a Web API provides flexibility to any style of communication. Whereas a REST API can take advantage of using REST, SOAP, and XML-RPC for communication.


3) Web API vs REST API: Design

As Web APIs are lightweight architecture, they are designed for gadgets constrained to devices like smartphones. In contrast, REST APIs send and receive data over systems making it a complex architecture.


4) Web API vs REST API: Support

Web API can be hosted only on an Internet Information Service (IIS) or self that supports XML and JSON requests. In contrast, REST API can be hosted only on IIS that supports standardized XML requests.

Difference Between Code First Approach And Database First Approach

 Code First Approach

In code first approach, we will first create entity classes with properties defined in it. Entity framework will create the database and tables based on the entity classes defined. So database is generated from the code. When the dot net code is run database will get created.

Advantages

1.      You can create  the database and tables from your business objects.

2.      You can specify which related collections are to be eager loaded or not be serialized at all.

3.      Database version control.

4.      Good for small applications.

Disadvantages

1.      You have to write everything related to database in the visual studio code.

2.      For stored procedures you have to map stored procedure using Fluent API and write Stored Procedure inside the code.

3.      If you want to change anything in the database tables you to make changes in the entity classes in the code file and run the update-database from the  package manager console.

4.      Not preferred for Data intensive applications. 


Database First Approach

In this approach Database and tables are created first. Then you create entity Data Model using the created database.

Advantages

1.      Simple to create the data model

2.      Graphical  user interface.

3.      Mapping and creation of keys and relationships are easy as you need not have to write any code .

4.      Preferred for data intense and large applications

Disadvantages

1.      Using an existing database to generate a .edmx model file and the associated code models results in a giant pile of auto generated code.

2.      When you need to add any functionality to generated model you have to extend the model class generated.