.Netcore Api + angular 6 + EFCore Project Part - 1
Important Configuration Need to be Done In AppSettins.json File And Startup file
==============================================================
=============================Startup file=======================
==============================================================
namespace DntAppApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//Need To Add For Configuration Of Connection String With DbContext
services.AddDbContext<MPContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//Need To Add Service And implementation To Use In Our Application
services.AddScoped<IProduct_Repository, Product_Repository>();
// Need To Add For Cross Origin Resourse Sharing Calls
services.AddCors(
options => {
options.AddPolicy(
"MAYUR", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request //pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
// Need To Add For Cross Origin Resourse Sharing Calls
app.UseCors("MAYUR");
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
==============================================================
==========================AppSettins.Json file==================
==============================================================
{
//Add ConnectionStrings Like this To Use It In Application
"ConnectionStrings": {
"DefaultConnection": "data source=DESKTOP-4VG8BV2\\SQLEXPRESS; initial catalog=MP;Integrated Security=true;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
==============================================================
=====================ValuesController==========================
==============================================================
namespace DntAppApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
IProduct_Repository _IProduct_Repository = new Product_Repository(new MPContext());
[HttpGet]
public IQueryable<TblProduct> Get()
{
return _IProduct_Repository.GetAll();
}
[HttpPost]
public TblProduct Post(TblProduct obj)
{
_IProduct_Repository.Save(obj);
return obj;
}
[HttpPut]
public TblProduct Put(TblProduct obj)
{
_IProduct_Repository.Save(obj);
return obj;
}
[HttpDelete]
public int Delete(int Productid)
{
_IProduct_Repository.Remove(Productid);
return 1;
}
}
}
Important Configuration Need to be Done In AppSettins.json File And Startup file
==============================================================
=============================Startup file=======================
==============================================================
namespace DntAppApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
//Need To Add For Configuration Of Connection String With DbContext
services.AddDbContext<MPContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//Need To Add Service And implementation To Use In Our Application
services.AddScoped<IProduct_Repository, Product_Repository>();
// Need To Add For Cross Origin Resourse Sharing Calls
services.AddCors(
options => {
options.AddPolicy(
"MAYUR", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request //pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
// Need To Add For Cross Origin Resourse Sharing Calls
app.UseCors("MAYUR");
app.UseHttpsRedirection();
app.UseMvc();
}
}
}
==============================================================
==========================AppSettins.Json file==================
==============================================================
{
//Add ConnectionStrings Like this To Use It In Application
"ConnectionStrings": {
"DefaultConnection": "data source=DESKTOP-4VG8BV2\\SQLEXPRESS; initial catalog=MP;Integrated Security=true;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
==============================================================
=====================ValuesController==========================
==============================================================
namespace DntAppApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
IProduct_Repository _IProduct_Repository = new Product_Repository(new MPContext());
[HttpGet]
public IQueryable<TblProduct> Get()
{
return _IProduct_Repository.GetAll();
}
[HttpPost]
public TblProduct Post(TblProduct obj)
{
_IProduct_Repository.Save(obj);
return obj;
}
[HttpPut]
public TblProduct Put(TblProduct obj)
{
_IProduct_Repository.Save(obj);
return obj;
}
[HttpDelete]
public int Delete(int Productid)
{
_IProduct_Repository.Remove(Productid);
return 1;
}
}
}
No comments:
Post a Comment