1. Create Database
2. Add Tabel to created database
3. Add packages to api project microsoft.entityframeworkcore.
4. Make InvariantGlobalization False in .csproj entry
5. Run Following command in Package Manager Console to add models classes
Scaffold-DbContext "Server=AVINASH-PC\SQLEXPRESS;Database=DBFirst;Trusted_Connection=True;TrustServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
6. Remove Connection string from dbcontext and add it to appsettngs.json file
7. Register connection string in program.cs file
var provider = builder.Services.BuildServiceProvider();
var config = provider.GetRequiredService();
builder.Services.AddDbContext(item => item.UseSqlServer(config.GetConnectionString("dbcs")));
or
builder.Services.AddDbContext(options => options.UseSqlServer(builder.Configuration.GetConnectionString("dbcs")));
- Add Api Controller
- Add cors policy to program file
- add this line on top
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCors(options =>
{
options.AddPolicy(name: MyAllowSpecificOrigins,
policy =>
{
policy.WithOrigins("http://localhost:7059", "https://localhost:7059")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
- add this line to below app.UserHttpRedirection
app.UseCors(MyAllowSpecificOrigins);
Fetch API
Get Request
let response = await fetch("https://localhost:7111/api/Emp/getallemployee")
if (response.ok) {
let employees = await response.json();
}
POST Request
// Make a POST request using the Fetch API
fetch('https://localhost:7111/api/Emp/createemployee', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(obj)
})
.then(res => res.json())
.then(data => {
})
Delete Request
fetch('https://localhost:7111/api/Emp/deleteemployee/' + id, {
method: 'DELETE',
})
.then(res => res.json())
.then(data => {
})
-
Notifications
You must be signed in to change notification settings - Fork 0
avinashkoshti-dev/DotNetCRUD
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published