Corer 2.2
Corer 2.2
Corer 2.2
TABLE OF CONTENTS
https://www.youtube.com/playlist?list=PLUOequmGnXxOgmSDWU7Tl6iQTsOtyjtwU ........................................... 1
ASP.NET Core 2.2 REST API Tutorial 1 - Setup and Swagger configuration ................................................................... 2
ASP.NET Core 2.2 REST API Tutorial 2 - Implementing versioning................................................................................. 4
ASP.NET Core 2.2 REST API Tutorial 3 - Dependency injection and clean service registration ..................................... 5
ASP.NET Core 3 & 2.2 REST API Tutorial 4 - Creating resources using POST ................................................................. 7
ASP.NET Core 3 & 2.2 REST API Tutorial 5 - Retrieving resources with GET .................................................................. 8
ASP.NET Core 2.2 REST API Tutorial 6 - Updating resources with PUT ........................................................................ 11
ASP.NET Core 3 & 2.2 REST API Tutorial 7 - Deleting resources with DELETE ............................................................. 12
ASP.NET CORE 2.2 REST API TUTORIAL 1 - SETUP AND SWAGGER CONFIGURATION
public PostsController()
{
_posts = new List<Post>();
for (var i = 0; i < 5; i++)
{
_posts.Add(new Post{Id = Guid.NewGuid().ToString()});
}
}
[HttpGet(ApiRoutes.Posts.GetAll)]
public IActionResult GetAll()
{
return Ok(_posts);
}
}
}
ASP.NET CORE 2.2 REST API TUTORIAL 3 - DEPENDENCY INJECTION AND CLEAN SERVICE
REGISTRATION
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(x =>
{
x.SwaggerDoc("v1", new Info { Title = "Tweetbook API", Version = "v1" });
});
}
}
if (string.IsNullOrEmpty(post.Id))
post.Id = Guid.NewGuid().ToString();
_posts.Add(post);
List<Post> GetPosts();
}
Add PostService.cs class which implements IPostService interface
public class PostService : IPostService
{
private readonly List<Post> _posts;
public PostService()
{
_posts = new List<Post>();
for (var i = 0; i < 5; i++)
{
_posts.Add(new Post
{
Id = Guid.NewGuid(),
Name = $"Post Name {i}"
});
}
}
public List<Post> GetPosts()
{
return _posts;
}
}
}
Modify DBInstaller.cs (which was created instead of startup.cs) and add AddSingleton method
public void InstallServices(IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
configuration.GetConnectionString("DefaultConnection")));
services.AddSingleton<IPostService, PostService>();
}
Modify PostsController.cs, as follows
public class PostsController : Controller
{
[HttpGet(ApiRoutes.Posts.GetAll)]
public IActionResult GetAll()
{
return Ok(_postService.GetPosts());
}
[HttpGet(ApiRoutes.Posts.Get)]
public IActionResult Get([FromRoute] Guid postId)
{
var post = _postService.GetPostById(postId);
if (post == null)
return NotFound();
return Ok(post);
}
[HttpPost(ApiRoutes.Posts.Create)]
public IActionResult Create([FromBody] CreatePostRequest postRequest)
{
var post = new Post{Id = postRequest.Id};
if (post.Id != Guid.Empty)
post.Id = Guid.NewGuid();
_postService.GetPosts().Add(post);
}
ASP.NET CORE 2.2 REST API TUTORIAL 6 - UPDATING RESOURCES W ITH PUT
[HttpPut(ApiRoutes.Posts.Update)]
public IActionResult Update([FromRoute] Guid postId, [FromBody] UpdatePostRequest request)
{
var post = new Post
{
Id = postId,
Name = request.Name
};
var updated = _postService.UpdatePost(post);
if (!updated)
return NotFound();
return Ok(post);
}
ASP.NET CORE 3 & 2.2 REST API TUTORIAL 7 - DELETING RESOURCES W ITH DELETE
[HttpDelete(ApiRoutes.Posts.Delete)]
public IActionResult Delete([FromRoute] Guid postId)
{
var isDeleted = _postService.DeletePost(postId);
if (!isDeleted)
return NotFound();
return NoContent();
}