This project will demonstrate how to Use PUT Method in ASP.NET Core Web API
LearnASPNETCoreWebAPIWithRealApps
Controllers
ProductController.cs
Models
Product.cs
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace LearnASPNETCoreWebAPIWithRealApps.Models
{
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public double Price { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using LearnASPNETCoreWebAPIWithRealApps.Models;
namespace LearnASPNETCoreWebAPIWithRealApps.Controllers
{
[Route("api/product")]
public class ProductController : Controller
{
[Consumes("application/json")]
[Produces("application/json")]
[HttpPut("update")]
public async Task<IActionResult> Update([FromBody] Product product)
{
try
{
Debug.WriteLine("Update Product Information");
Debug.WriteLine("Id: " + product.Id);
Debug.WriteLine("Name: " + product.Name);
Debug.WriteLine("Price: " + product.Price);
return Ok(product);
}
catch
{
return BadRequest();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace LearnASPNETCoreWebAPIWithRealApps
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
}